%% welfare analysis

start_period = (age_sims_br == 1);
sample_half = round(sim_periods/2);

start_period( 1:sample_half,:)  = 0;
start_period = zeros(size(age_sims_br));
start_period(sample_half+1,:) = 1;

end_period   = zeros(size(start_period));

total_agents = sum(start_period(:));

U_br              = NaN(total_agents,1);
U_re              = U_br;
%U_re_welf         = U_br;
U_br_star         = U_br;
U_re_rtbr         = U_br;
U_br_rtbr         = U_br;

signal_pos   = NaN(sim_periods, total_agents);

life_length = U_br;
signal_num  = U_br;

c_sims_br        = NaN(sim_periods, total_agents);
c_sims_re        = NaN(sim_periods, total_agents);
%c_sims_re_welf   = NaN(sim_periods, total_agents);
c_sims_br_star   = NaN(sim_periods, total_agents);
c_sims_re_rtbr   = NaN(welf_sim_size, total_agents);
c_sims_br_rtbr   = NaN(welf_sim_size, total_agents);

mpc_reg_br  = NaN(total_agents, 1);
mpc_reg_re  = NaN(total_agents, 1);

mpc_simple_br  = NaN(total_agents, 1);
mpc_simple_re  = NaN(total_agents, 1);



iter = 0;

for i = 1:nsim
    
    ind_start = find(start_period(:,i) == 1);
    ind_end   = [ind_start(2:end)-1;sim_periods];
    
    for j = 1:length(ind_start)
        
        iter = iter + 1;
        
        life_length(iter)  = age_sims_br(ind_end(j),i);
        life_length(iter)  = sim_periods - sample_half;
        
        signal_num(iter) = sum(sigma_etasq_sims(ind_start(j):ind_end(j),i) < 1e10);
        
        ya_temp = a_sims_br(ind_start(j):ind_end(j),i)*(1+rt_br) + w_br*yi(ind_start(j):ind_end(j),i);
        temp = ya_temp(sigma_etasq_sims(ind_start(j):ind_end(j),i) < 1e10);
        
        signal_pos(1:length(temp),i) = temp;
        
        ctemp = chat_sims_br( ind_start(j):ind_end(j), i);
        utemp = util(ctemp, util_type, gam);
        
        U_br(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        if life_length(iter) > 10
            [b_temp,~,~,~,~] = regress( log(ctemp), [ones(length(ctemp),1), log(yi(ind_start(j):ind_end(j),i))]);
            mpc_reg_br(iter) = b_temp(2);
        end
        
        
        c_sims_br(1:life_length(iter), iter) = ctemp;
        
        ctemp = cstar_sims_re( ind_start(j):ind_end(j), i);
        utemp = util(ctemp, util_type, gam);
        
        if life_length(iter) > 10
            [b_temp,~,~,~,~] = regress( log(ctemp), [ones(length(ctemp),1), log(yi(ind_start(j):ind_end(j),i))]);
            mpc_reg_re(iter) = b_temp(2);
        end
        
        U_re(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        c_sims_re(1:life_length(iter), iter) = ctemp;
        
        %{
        ctemp = cstar_sims_re_welf( ind_start(j):ind_end(j), i);
        utemp = util(ctemp, util_type, gam);
      
        
        U_re_welf(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        c_sims_re_welf(1:life_length(iter), iter) = ctemp;
        %}
        
        ctemp = cstar_sims_br( ind_start(j):ind_end(j), i);
        utemp = util(ctemp, util_type, gam);
        
        U_br_star(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        c_sims_br_star(1:life_length(iter), iter) = ctemp;
        
        %%%%%% same rt, but different asset evolution -- RE
        ctemp = cstar_sims_re_rtbr( :, i);
        utemp = util(ctemp, util_type, gam);
        
        U_re_rtbr(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        %%%%%% same rt, but different asset evolution -- BR
        ctemp = chat_sims_br( sim_periods - welf_sim_size+1:end, i);
        utemp = util(ctemp, util_type, gam);
        
        U_br_rtbr(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        
        
    end
    
end
%%%
%log(c)/(1-bta) = U0;

bta_mult = (1 - btat.^(life_length+1))/(1-btat);

bta_mult_rtbr = (1 - btat.^(welf_sim_size+1))/(1-btat);
%bta_mult = 1/(1-bta);

if gam == 1
    cons_equiv_re      = exp(U_re./bta_mult);
    cons_equiv_br      = exp(U_br./bta_mult);
    %cons_equiv_re_welf = exp(U_re_welf./bta_mult);
    cons_equiv_br_star = exp(U_br_star./bta_mult);
    
    cons_equiv_re_rtbr = exp(U_re_rtbr./bta_mult_rtbr);
    cons_equiv_br_rtbr = exp(U_br_rtbr./bta_mult_rtbr);
    
else
    cons_equiv_re      = (U_re./bta_mult*(1-gam) + 1).^(1/(1-gam));
    cons_equiv_br      = (U_br./bta_mult*(1-gam) + 1).^(1/(1-gam));
    %cons_equiv_re_welf = (U_re_welf./bta_mult*(1-gam) + 1).^(1/(1-gam));
    cons_equiv_br_star = (U_br_star./bta_mult*(1-gam) + 1).^(1/(1-gam));
    
    cons_equiv_re_rtbr = (U_re_rtbr/bta_mult_rtbr*(1-gam) + 1).^(1/(1-gam));
    cons_equiv_br_rtbr = (U_br_rtbr/bta_mult_rtbr*(1-gam) + 1).^(1/(1-gam));
    
end

start_period = (age_sims_br == 1);

%%%
wloss = -log(cons_equiv_br./cons_equiv_re);
%wloss_samer = -log(cons_equiv_br./cons_equiv_re_welf);
wloss_brstar = -log(cons_equiv_br./cons_equiv_br_star);
wloss_rtbr = -log(cons_equiv_br_rtbr./cons_equiv_re_rtbr);

avg_wloss_aquint    = NaN(5,1);
quint_temp          = prctile( a_sims_br(sim_periods - welf_sim_size+1,:), 100*[0.2, 0.4, 0.6, 0.8]);
avg_wloss_aquint(1) =  mean(wloss_rtbr(a_sims_br(sim_periods - welf_sim_size+1,:) <= quint_temp(1)));
avg_wloss_aquint(2) =  mean(wloss_rtbr( a_sims_br(sim_periods - welf_sim_size+1,:) > quint_temp(1) &  a_sims_br(sim_periods - welf_sim_size+1,:) <= quint_temp(2)));
avg_wloss_aquint(3) =  mean(wloss_rtbr( a_sims_br(sim_periods - welf_sim_size+1,:) > quint_temp(2) &  a_sims_br(sim_periods - welf_sim_size+1,:) <= quint_temp(3)));
avg_wloss_aquint(4) =  mean(wloss_rtbr( a_sims_br(sim_periods - welf_sim_size+1,:) > quint_temp(3) &  a_sims_br(sim_periods - welf_sim_size+1,:) <= quint_temp(4)));
avg_wloss_aquint(5) =  mean(wloss_rtbr( a_sims_br(sim_periods - welf_sim_size+1,:) > quint_temp(4)));

disp('                 kappa     sigma_c     psi')
disp([' Parameters:     ',  sprintf('%1.4f    ', [kappa sigma_c psi])])
disp(' ');
disp(' --------------------------------')
disp([' Moments '])
disp(' --------------------------------')
disp(['                       '  ]);
disp( 'WELFARE LOSS                                Post.  ');
disp(['   Mean consumption loss (%)                  :' , sprintf('%1.4f   ',100*[mean(log( cons_equiv_re./cons_equiv_br))])  ]);
disp(['   Std. Dev consumption loss (%)              :' , sprintf('%1.4f   ',100*[std(log( cons_equiv_re./cons_equiv_br))]) ]);
disp(['   Mean consumption loss (same r & a0) (%)    :' , sprintf('%1.4f   ',100*[mean(log( cons_equiv_br_star./cons_equiv_br))])  ]);
disp(['   Std. Dev consumption loss (same r & a) (%) :' , sprintf('%1.4f   ',100*[std(log( cons_equiv_br_star./cons_equiv_br))]) ]);
disp(['   Mean consumption loss (same r) (%)         :' , sprintf('%1.4f   ',100*[mean(log( cons_equiv_re_rtbr./cons_equiv_br_rtbr))])  ]);
disp(['   Std. Dev consumption loss (same r) (%)     :' , sprintf('%1.4f   ',100*[std(log( cons_equiv_re_rtbr./cons_equiv_br_rtbr))]) ]);
disp(['   Mean consumption loss (same r) | Q1(a) (%) :' , sprintf('%1.4f   ',100*avg_wloss_aquint(1) )  ]);
disp(['   Mean consumption loss (same r) | Q2(a) (%) :' , sprintf('%1.4f   ', 100*avg_wloss_aquint(2) )  ]);
disp(['   Mean consumption loss (same r) | Q3(a) (%) :' , sprintf('%1.4f   ', 100*avg_wloss_aquint(3) )  ]);
disp(['   Mean consumption loss (same r) | Q4(a) (%) :' , sprintf('%1.4f   ', 100*avg_wloss_aquint(4) )  ]);
disp(['   Mean consumption loss (same r) | Q5(a) (%) :' , sprintf('%1.4f   ', 100*avg_wloss_aquint(5) )  ]);

disp(' ');
disp(' --------------------------------')
disp([' Percentiles of Welfare Loss Distribution '])
disp(' --------------------------------')
disp( 'WELFARE LOSS                          1     5      10      25      50    75    90    95   99');
disp(['  Welfare Loss (%)               :' , sprintf('%1.2f   ', 100*[prctile(wloss, [1,5,10,25,50,75,90,95,99])])  ]);
disp(['  Welfare Loss (same r & a) (%)  :' , sprintf('%1.2f   ', 100*[prctile(wloss_brstar, [1,5,10,25,50,75,90,95,99])])  ]);
disp(['  Welfare Loss (same r) (%)      :' , sprintf('%1.2f   ', 100*[prctile(wloss_rtbr, [1,5,10,25,50,75,90,95,99])])  ]);


%%%
figure
hold on
[h, stats_re] = cdfplot( a_sims_re(end,:)');
[h, stats_br] = cdfplot( a_sims_br(end,:)');
hold off


sorted_wealth_br = NaN(sim_periods - sample_half +1, nsim);
sorted_wealth_re = NaN(sim_periods - sample_half +1, nsim);
sorted_wealth_tr = NaN(sim_periods - sample_half +1, nsim);

for i = 1:sim_periods - sample_half+1
    sorted_wealth_br(i,:) = sort( a_sims_br( sample_half -1 + i,:));
   %sorted_wealth_re(i,:) = sort( a_sims_re( sample_half -1 + i,:));
   %sorted_wealth_tr(i,:) = sort( a_sims_tr( sample_half -1 + i,:));
    
end

wealth_dist_br = mean(sorted_wealth_br)';
wealth_dist_re = mean(sorted_wealth_re)';
wealth_dist_tr = mean(sorted_wealth_tr)';

wealth_dist_br_all = nanmean(a_sims_br)';
wealth_dist_re_all = nanmean(a_sims_re)';
wealth_dist_tr_all = nanmean(a_sims_tr)';

xi_br = linspace(0,0.15,2);
xi_br = [xi_br, linspace(0.15,max(wealth_dist_br),50)];
xi_br = unique(xi_br);

figure
h = histogram( wealth_dist_br,xi_br,'EdgeColor','b','Normalization','probability','DisplayStyle','stairs');
hold on
wealth_pdf_br = h.Values./sum(h.Values);
xi_br = h.BinEdges;
xi_br = mean([xi_br(1:end-1)',xi_br(2:end)'],2);

figure
h_re = histogram(wealth_dist_re,h.BinEdges,'EdgeColor','r','Normalization','probability','DisplayStyle','stairs');
wealth_pdf_re = h_re.Values./sum(h_re.Values);
hold off

figure
%plot(xi_br, wealth_pdf_re, 'Color', [0.85, 0.325, 0.098],'LineWidth',2.5)
%hold on
%plot(xi_br , wealth_pdf_br, 'Color', [0, 0.447, 0.741],'LineWidth',2.5)
%hold off
plot(xi_br, [wealth_pdf_re',wealth_pdf_br'],'LineWidth',2.5)
xlim([min(xi_br)-0.5, max(xi_br)])
ylim([0 0.23])
xlabel('Asset holding (a)')
ylabel('Probability density')
legend('FI','BR','FontSize',13)
title('Wealth Distribution')
ax = gca;
ax.FontSize = 12;
print -dpsc wealth_distq0.eps

%{

[cdf_br, cdfx_br] = ecdf(wealth_dist_br);
[cdf_re, cdfx_re] = ecdf(wealth_dist_re);

xbins_br = round(linspace( 1, length(cdfx_br),101));
xi_br = cdfx_br(xbins_br);

[wealth_pdf_br, xi_br ] = hist(wealth_dist_br);
[wealth_pdf_re, xi_re] = hist(wealth_dist_re,xi_br);

figure
plot(xi_br, wealth_pdf_br/sum(wealth_pdf_br))

figure
h = histogram( wealth_dist_br,'EdgeColor','b','Normalization','probability','DisplayStyle','stairs');

wealth_pdf_br = h.Values./sum(h.Values);
xi_br = h.BinEdges;
xi_br = mean([xi_br(1:end-1)',xi_br(2:end)'],2);

h_re = histogram(wealth_dist_re,h.BinEdges,'EdgeColor','r','Normalization','probability','DisplayStyle','stairs');
wealth_pdf_re = h_re.Values./sum(h_re.Values);

figure
plot(xi_br, [wealth_pdf_br', wealth_pdf_re'], 'LineWidth',2)

hold on
%yline(1,'--k')
%yline(0.5,'--k')
%ylim([0, 0.21])
%xlim([-0.3  20])
hold off
xlabel('Asset holding (a)')
ylabel('Probability mass')
legend('A^{bc}','A^{RE}')
title('Borrowing Constraint Model - Wealth Distribution')

%}
figure
h = histogram( wealth_dist_br(wealth_dist_br > 0),100,'EdgeColor','b','Normalization','probability','DisplayStyle','stairs');
hold on
histogram(wealth_dist_re(wealth_dist_re > 0),h.BinEdges,'EdgeColor','r','Normalization','probability','DisplayStyle','stairs');
%yline(1,'--k')
%yline(0.5,'--k')
%xlim([0, 10])
hold off
xlabel('Asset holding (a)')
ylabel('Probability mass')
legend('A^{bc}','A^{RE}')
title('Borrowing Constraint Model - Wealth Distribution')

%[cdf_m6, x_m6] = ecdf(wealth_dist_m6);
%[cdf_m8, x_m8] = ecdf(wealth_dist_m8);

wealth_grid = linspace( 0, 5*amax, stateGrid_size)';

cdf_br = NaN(stateGrid_size,1);
cdf_re = cdf_br;

cdf_br_all = NaN(stateGrid_size,1);
cdf_re_all = cdf_br;

cdf_br_mat = NaN(sim_periods - sample_half +1, stateGrid_size);
cdf_re_mat = NaN(sim_periods - sample_half +1, stateGrid_size);


for i = 1:length(wealth_grid)
    
    cdf_br(i) = sum( wealth_dist_br <= wealth_grid(i))./nsim;
    cdf_re(i) = sum( wealth_dist_re <= wealth_grid(i))./nsim;
    
    cdf_br_all(i) = sum( wealth_dist_br_all <= wealth_grid(i))./total_agents;
    cdf_re_all(i) = sum( wealth_dist_re_all <= wealth_grid(i))./total_agents;
    
    cdf_br_mat(:,i) = sum( a_sims_br( sample_half:end,:) <= wealth_grid(i),2)./nsim;
    cdf_re_mat(:,i) = sum( a_sims_re( sample_half:end,:) <= wealth_grid(i),2)./nsim;
    
end

gini_br_mat = NaN(sim_periods - sample_half + 1,1);
gini_re_mat = NaN(sim_periods - sample_half + 1,1);
gini_tr_mat = NaN(sim_periods - sample_half + 1,1);


for i = 1:sample_half + 1
    
    gini_br_mat(i) = gini( ones(nsim,1), (a_sims_br( sample_half - 1 + i,:).*(a_sims_br(sample_half - 1 + i,:)>=0))');
    gini_re_mat(i) = gini( ones(nsim,1), (a_sims_re( sample_half - 1 + i,:).*(a_sims_re(sample_half - 1 + i,:)>=0))');
    gini_tr_mat(i) = gini( ones(nsim,1), (a_sims_tr( sample_half - 1 + i,:).*(a_sims_tr(sample_half - 1 + i,:)>=0))');
    
end

cdf_br_mean = mean(cdf_br_mat)';
cdf_re_mean = mean(cdf_re_mat)';

gini_br_mean = mean(gini_br_mat);
gini_re_mean = mean(gini_re_mat);
gini_tr_mean = mean(gini_tr_mat);

figure
plot( wealth_grid, [cdf_br, cdf_re])

figure
plot( wealth_grid, [cdf_br, cdf_br_mean])

%[gini( ones(nsim,1), wealth_dist_br), gini( ones(nsim,1), wealth_dist_re)];
%[gini_br_mean, gini_re_mean];

perc99_br = prctile( wealth_dist_br, 99);
perc99_re = prctile( wealth_dist_re, 99);
perc99_tr = prctile( wealth_dist_tr, 99);

perc95_br = prctile( wealth_dist_br, 95);
perc95_re = prctile( wealth_dist_re, 95);
perc95_tr = prctile( wealth_dist_tr, 95);

perc90_br = prctile( wealth_dist_br, 90);
perc90_re = prctile( wealth_dist_re, 90);
perc90_tr = prctile( wealth_dist_tr, 90);

wealthsh_top1_br = sum(wealth_dist_br( wealth_dist_br >= perc99_br))/sum(wealth_dist_br);
wealthsh_top1_re = sum(wealth_dist_re( wealth_dist_re >= perc99_re))/sum(wealth_dist_re);
wealthsh_top1_tr = sum(wealth_dist_tr( wealth_dist_tr >= perc99_tr))/sum(wealth_dist_tr);

wealthsh_top5_br = sum(wealth_dist_br( wealth_dist_br >= perc95_br))/sum(wealth_dist_br);
wealthsh_top5_re = sum(wealth_dist_re( wealth_dist_re >= perc95_re))/sum(wealth_dist_re);
wealthsh_top5_tr = sum(wealth_dist_tr( wealth_dist_tr >= perc95_tr))/sum(wealth_dist_tr);


wealthsh_top10_br = sum(wealth_dist_br( wealth_dist_br >= perc90_br))/sum(wealth_dist_br);
wealthsh_top10_re = sum(wealth_dist_re( wealth_dist_re >= perc90_re))/sum(wealth_dist_re);
wealthsh_top10_tr = sum(wealth_dist_tr( wealth_dist_tr >= perc90_tr))/sum(wealth_dist_tr);



agentsh_zeroa_br = sum(  wealth_dist_br == 0)/nsim;
agentsh_zeroa_re = sum(  wealth_dist_re == 0)/nsim;
agentsh_zeroa_re = sum(  wealth_dist_tr == 0)/nsim;

handtomouthsh_br = sum( wealth_dist_br < 0.25*median(wealth_dist_br))/nsim;
handtomouthsh_re = sum( wealth_dist_re < 0.25*median(wealth_dist_re))/nsim;

handtomouthsh_br = sum( wealth_dist_br <= w_br/6)/nsim;
handtomouthsh_re = sum( wealth_dist_re <= w_br/6)/nsim;
handtomouthsh_tr = sum( wealth_dist_tr <= w_tr/6)/nsim;

nega_br = sum( wealth_dist_br < 0)/nsim;
nega_re = sum( wealth_dist_re < 0)/nsim;

mean( wealth_dist_br( wealth_dist_br <= w_br/6)./(rt_br*wealth_dist_br(wealth_dist_br <= w_br/6) + w_br) );
mean( wealth_dist_br( wealth_dist_br > w_br/6)./(rt_br*wealth_dist_br(wealth_dist_br > w_br/6) + w_br) )
mean( wealth_dist_re( wealth_dist_re > w_re/6)./(rt_re*wealth_dist_re(wealth_dist_re > w_re/6) + w_re) )
mean( wealth_dist_re( wealth_dist_re <= w_re/6)./(rt_re*wealth_dist_re(wealth_dist_re <= w_re/6) + w_re) )

%a_small = ya_cons_binds - exp(ybar_logn - sigma_yi - sigma_y);
%agentsh_smalla_m6 = sum(  wealth_dist_br <= a_small)/nsim;
%agentsh_smalla_m8 = sum(  wealth_dist_re <= a_small)/nsim;
%agentsh_smalla_m10 = sum(  wealth_dist_m10 <= a_small)/nsim;

wealth_quint_br = prctile( wealth_dist_br, 100*[0.2, 0.4, 0.6, 0.8]);
wealth_quint_re = prctile( wealth_dist_re, 100*[0.2, 0.4, 0.6, 0.8]);
wealth_quint_tr = prctile( wealth_dist_tr, 100*[0.2, 0.4, 0.6, 0.8]);

wealth_sh_quint_br = NaN( length(wealth_quint_br) + 1 ,1);
wealth_sh_quint_re = NaN( length(wealth_quint_br) + 1  ,1);
wealth_sh_quint_tr = NaN( length(wealth_quint_tr) + 1  ,1);

wealth_sh_quint_br(1) = sum( wealth_dist_br( wealth_dist_br <= wealth_quint_br(1)))/sum(wealth_dist_br);
wealth_sh_quint_re(1) = sum( wealth_dist_re( wealth_dist_re <= wealth_quint_re(1)))/sum(wealth_dist_re);
wealth_sh_quint_tr(1) = sum( wealth_dist_tr( wealth_dist_tr <= wealth_quint_tr(1)))/sum(wealth_dist_tr);


for i = 2:length(wealth_quint_br)
    
    wealth_sh_quint_br(i) = sum( wealth_dist_br( wealth_dist_br <= wealth_quint_br(i)))/sum(wealth_dist_br) - sum(wealth_sh_quint_br(1:i-1)) ;
    wealth_sh_quint_re(i) = sum( wealth_dist_re( wealth_dist_re <= wealth_quint_re(i)))/sum(wealth_dist_re) - sum(wealth_sh_quint_re(1:i-1));
    wealth_sh_quint_tr(i) = sum( wealth_dist_tr( wealth_dist_tr <= wealth_quint_tr(i)))/sum(wealth_dist_tr) - sum(wealth_sh_quint_tr(1:i-1));
    
end

wealth_sh_quint_br(end) = sum( wealth_dist_br( wealth_dist_br > wealth_quint_br(end)))/sum(wealth_dist_br);
wealth_sh_quint_re(end) = sum( wealth_dist_re( wealth_dist_re > wealth_quint_re(end)))/sum(wealth_dist_re);
wealth_sh_quint_tr(end) = sum( wealth_dist_tr( wealth_dist_tr > wealth_quint_tr(end)))/sum(wealth_dist_tr);


%%%
%%%%% Social Mobility
%%% Quintile transition matrix

T_br = NaN( 5,5,sim_periods - sample_half);
T_re = NaN( 5,5,sim_periods - sample_half);

step = 36; 
step = 5;
%step = 1; 

tic
for i = 1:step:sim_periods - sample_half
    
    t = sample_half+i;
    new_agents_ind = find(reset_shocks(t,:) < deltax);
    
    if step > 1
       
        for j = 1:step - 1
            
           new_agents_ind = [new_agents_ind, find(reset_shocks(t-j,:) < deltax)];
            
        end
        
    end
    
    %new_agents_ind = []; 
    
    [q1_ind_br, q2_ind_br, q3_ind_br, q4_ind_br, q5_ind_br] = wealth_dist_prc_index( a_sims_br(t,:) , 100*[0.2, 0.4, 0.6, 0.8], nsim);
    [q1_ind_re, q2_ind_re, q3_ind_re, q4_ind_re, q5_ind_re] = wealth_dist_prc_index( a_sims_re(t,:) , 100*[0.2, 0.4, 0.6, 0.8], nsim);
    %[q1_ind_re, q2_ind_re, q3_ind_re, q4_ind_re, q5_ind_re] = wealth_dist_prc_index( a_sims_re(t,:) , 100*[0.01, 0.2, 0.6, 0.9], nsim);

    if i > 1
        
        q1_old_eff_br = setdiff(q1_ind_br_old, new_agents_ind);
        q2_old_eff_br = setdiff(q2_ind_br_old, new_agents_ind);
        q3_old_eff_br = setdiff(q3_ind_br_old, new_agents_ind);
        q4_old_eff_br = setdiff(q4_ind_br_old, new_agents_ind);
        q5_old_eff_br = setdiff(q5_ind_br_old, new_agents_ind);
        
        q1_old_eff_re = setdiff(q1_ind_re_old, new_agents_ind);
        q2_old_eff_re = setdiff(q2_ind_re_old, new_agents_ind);
        q3_old_eff_re = setdiff(q3_ind_re_old, new_agents_ind);
        q4_old_eff_re = setdiff(q4_ind_re_old, new_agents_ind);
        q5_old_eff_re = setdiff(q5_ind_re_old, new_agents_ind);
        
        
        
        T_br(:,:,i) = quint_trans_mat( q1_old_eff_br, q2_old_eff_br, q3_old_eff_br, q4_old_eff_br, q5_old_eff_br, q1_ind_br, q2_ind_br, q3_ind_br, q4_ind_br, q5_ind_br);
        T_re(:,:,i) = quint_trans_mat( q1_old_eff_re, q2_old_eff_re, q3_old_eff_re, q4_old_eff_re, q5_old_eff_re, q1_ind_re, q2_ind_re, q3_ind_re, q4_ind_re, q5_ind_re);
        
        
    end
    
    q1_ind_br_old = q1_ind_br;
    q2_ind_br_old = q2_ind_br;
    q3_ind_br_old = q3_ind_br;
    q4_ind_br_old = q4_ind_br;
    q5_ind_br_old = q5_ind_br;
    
    q1_ind_re_old = q1_ind_re;
    q2_ind_re_old = q2_ind_re;
    q3_ind_re_old = q3_ind_re;
    q4_ind_re_old = q4_ind_re;
    q5_ind_re_old = q5_ind_re;
    
end
toc

T_br_mean = nanmean( T_br, 3);
s_br = (5 - sum( diag(T_br_mean)))/4;
T_re_mean = nanmean( T_re, 3);
s_re = (5 - sum( diag(T_re_mean)))/4;

T_br_5yr_mean = T_br_mean^5;
%{
T_br_5yr_mean = [ 0.36, 0.29, 0.16, 0.12, 0.07; 
                   0.26 0.24 0.26 0.15 0.12; 
                   0.16 0.21 0.25 0.24 0.15; 
                   0.15 0.13 0.2 0.26 0.26; 
                   0.11 0.16 0.14 0.24 0.36]; 
 %}              

s_br_5yr = (5 - sum( diag(T_br_5yr_mean)))/4;
T_re_5yr_mean = T_re_mean^5;
s_re_5yr = (5 - sum( diag(T_re_5yr_mean)))/4;
%% APCs

htm_sims_br    = a_sims_br(sample_half:end,:) <= w_br.*yi(sample_half:end,:)/6;
htm_sims_re    = a_sims_re(sample_half:end,:) <= w_re.*yi(sample_half:end,:)/6;
htm_sims_tr    = a_sims_tr(sample_half:end,:) <= w_tr.*yi(sample_half:end,:)/6;

apc_sims_br    = chat_sims_br(sample_half:end,:)./( rt_br*a_sims_br(sample_half:end,:) + w_br*yi(sample_half:end,:));
apc_sims_re    = cstar_sims_re(sample_half:end,:)./( rt_re*a_sims_re(sample_half:end,:) + w_re*yi(sample_half:end,:));
apc_sims_tr    = cstar_sims_tr(sample_half:end,:)./( rt_tr*a_sims_tr(sample_half:end,:) + w_tr*yi(sample_half:end,:));

apc_sims_br_panel = apc_sims_br(3:end,:);
apc_sims_re_panel = apc_sims_re(3:end,:);
apc_sims_tr_panel = apc_sims_tr(3:end,:);

apc_sims_br_panel( age_sims_br(sample_half+2:end,:) <=2) = NaN; 
apc_sims_re_panel( age_sims_br(sample_half+2:end,:) <=2) = NaN; 
apc_sims_tr_panel( age_sims_br(sample_half+2:end,:) <=2) = NaN; 

htm_sims_br_panel = htm_sims_br(1:end-2,:);
htm_sims_br_panel = htm_sims_br_panel(:);


htm_sims_re_panel = htm_sims_re(1:end-2,:);
htm_sims_re_panel = htm_sims_re_panel(:);

htm_sims_tr_panel = htm_sims_tr(1:end-2,:);
htm_sims_tr_panel = htm_sims_tr_panel(:);

mpc_sims_br_panel = mpc_br2(sample_half+2:end,:);
mpc_sims_re_panel = mpc_re(sample_half+2:end,:);
mpc_sims_tr_panel = mpc_tr(sample_half+2:end,:);

mpc_sims_br_panel( age_sims_br(sample_half+2:end,:) <=2) = NaN; 
mpc_sims_re_panel( age_sims_br(sample_half+2:end,:) <=2) = NaN; 
mpc_sims_tr_panel( age_sims_br(sample_half+2:end,:) <=2) = NaN; 


%%%% Income and consumption growth regression

ygrowth_sims_br =log( (rt_br*a_sims_br(sample_half+2:end,:) + w_br.*yi(sample_half+2:end,:))./(rt_br*a_sims_br(sample_half:end-2,:) + w_br.*yi(sample_half:end-2,:)))/2;
ygrowth_sims_re =log( (rt_re*a_sims_re(sample_half+2:end,:) + w_re.*yi(sample_half+2:end,:))./(rt_re*a_sims_re(sample_half:end-2,:) + w_re.*yi(sample_half:end-2,:)))/2;
ygrowth_sims_tr =log( (rt_tr*a_sims_tr(sample_half+2:end,:) + w_tr.*yi(sample_half+2:end,:))./(rt_tr*a_sims_tr(sample_half:end-2,:) + w_tr.*yi(sample_half:end-2,:)))/2;
 
cgrowth_sims_br =log(chat_sims_br(sample_half+2:end,:)./(chat_sims_br(sample_half:end-2,:)))/2;
cgrowth_sims_re =log(cstar_sims_re(sample_half+2:end,:)./(cstar_sims_re(sample_half:end-2,:)))/2;
cgrowth_sims_tr =log(cstar_sims_tr(sample_half+2:end,:)./(cstar_sims_tr(sample_half:end-2,:)))/2;

cgrowth_sims_br( age_sims_br(sample_half+2:end,:) <=5) = NaN; 
cgrowth_sims_re( age_sims_br(sample_half+2:end,:) <=5) = NaN; 
cgrowth_sims_tr( age_sims_br(sample_half+2:end,:) <=5) = NaN; 

[yhtm_reg_br, bint, ~,~,~] = regress( ygrowth_sims_br(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:)]);
[yhtm_reg_re, bint, ~,~,~] = regress( ygrowth_sims_re(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:)]);
[yhtm_reg_tr, bint, ~,~,~] = regress( ygrowth_sims_tr(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:)]);

[chtm_reg_br, bint, ~,~,~] = regress( cgrowth_sims_br(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:)]);
[chtm_reg_re, bint, ~,~,~] = regress( cgrowth_sims_re(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:)]);
[chtm_reg_tr, bint, ~,~,~] = regress( cgrowth_sims_tr(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:)]);

[mpc_reg_br, bint, ~,~,~] = regress( mpc_sims_br_panel(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:)]);
[mpc_reg_re, bint, ~,~,~] = regress( mpc_sims_re_panel(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:)]);
[mpc_reg_tr, bint, ~,~,~] = regress( mpc_sims_tr_panel(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:)]);

%%% fixed effects

start_period = (age_sims_br == 1);
total_agents = sum(start_period(:));

life_length  = NaN(total_agents,1);
chtm_reg_indiv_br = NaN(total_agents,1);
chtm_reg_indiv_re = NaN(total_agents,1);
chtm_reg_indiv_tr = NaN(total_agents,1);

yhtm_reg_indiv_br = NaN(total_agents,1);
yhtm_reg_indiv_re = NaN(total_agents,1);
yhtm_reg_indiv_tr = NaN(total_agents,1);

frac_htm_status_br = NaN(size(a_sims_br));
frac_htm_status_re = NaN(size(a_sims_re));
frac_htm_status_tr = NaN(size(a_sims_re));

abar_br_sims = NaN(total_agents, 1);
rhoa_br_sims = NaN(total_agents, 1);
abar_re_sims = NaN(total_agents, 1);
rhoa_re_sims = NaN(total_agents, 1);

iter = 0;

for i = 1:nsim
    
    ind_start = find(start_period(:,i) == 1);
    ind_end   = [ind_start(2:end)-1;sim_periods];
    
    for j = 1:length(ind_start)
        
        if ind_start(j) < sample_half
            continue
        end
        
        iter = iter + 1;
        
        life_length(iter)  = age_sims_br(ind_end(j),i);
        
        ctemp         = chat_sims_br( ind_start(j):ind_end(j), i);
        ytemp         = w_br.*yi( ind_start(j):ind_end(j), i);
        cgrowth_temp  = log(ctemp(3:end)./ctemp(1:end-2))/2;
        ygrowth_temp  = log(ytemp(3:end)./ytemp(1:end-2))/2;
        
        htm_temp      = a_sims_br( ind_start(j):ind_end(j), i) <= w_br.*yi( ind_start(j):ind_end(j), i)/6;
        htm_temp      = a_sims_br( ind_start(j):ind_end(j), i) <= w_br/6;
        %frac_htm_status_br(ind_start(j):ind_end(j),i) = mean(htm_temp);
        
        if life_length(iter) > 5 && sum(htm_temp(1:end-2))> 0 && sum(~htm_temp(1:end-2)) > 0
            [b_temp,~,~,~,~] = regress( cgrowth_temp, [ones(length(cgrowth_temp),1), htm_temp(1:end-2)]);
            chtm_reg_indiv_br(iter) = b_temp(2);
            
            [b_temp,~,~,~,~] = regress( ygrowth_temp, [ones(length(cgrowth_temp),1), htm_temp(1:end-2)]);
            yhtm_reg_indiv_br(iter) = b_temp(2);
            frac_htm_status_br(ind_start(j):ind_end(j),i) = mean(htm_temp);
            
        end
        
        %if  life_length(iter) > 5
        %    frac_htm_status_br(ind_start(j):ind_end(j),i) = mean(htm_temp);
        %end
        
        ctemp         = cstar_sims_re( ind_start(j):ind_end(j), i);
        ytemp         = w_re.*yi( ind_start(j):ind_end(j), i);
        cgrowth_temp  =log(ctemp(3:end)./ctemp(1:end-2))/2;
        ygrowth_temp  = log(ytemp(3:end)./ytemp(1:end-2))/2;
        htm_temp      = a_sims_re( ind_start(j):ind_end(j), i) <= w_re.*yi( ind_start(j):ind_end(j), i)/6;
        %htm_temp      = a_sims_re( ind_start(j):ind_end(j), i) <= w_re/6;
 
        
        
        if life_length(iter) > 10 && sum(htm_temp(1:end-2))> 0 && sum(~htm_temp(1:end-2)) > 0
            [b_temp,~,~,~,~] = regress( cgrowth_temp, [ones(length(cgrowth_temp),1), htm_temp(1:end-2)]);
            chtm_reg_indiv_re(iter) = b_temp(2);
            
            [b_temp,~,~,~,~] = regress( ygrowth_temp, [ones(length(cgrowth_temp),1), htm_temp(1:end-2)]);
            yhtm_reg_indiv_re(iter) = b_temp(2);
            frac_htm_status_re(ind_start(j):ind_end(j),i) = mean(htm_temp);
            
        end
        
        %if  sum(htm_temp) > 2
        %    frac_htm_status_re(ind_start(j):ind_end(j),i) = mean(htm_temp);
        %end
        
        ctemp         = cstar_sims_tr( ind_start(j):ind_end(j), i);
        ytemp         = w_tr.*yi( ind_start(j):ind_end(j), i);
        cgrowth_temp  =log(ctemp(3:end)./ctemp(1:end-2))/2;
        ygrowth_temp  = log(ytemp(3:end)./ytemp(1:end-2))/2;
        htm_temp      = a_sims_tr( ind_start(j):ind_end(j), i) <= w_tr.*yi( ind_start(j):ind_end(j), i)/6;
        
        
        
        if life_length(iter) > 10 && sum(htm_temp(1:end-2))> 0 && sum(~htm_temp(1:end-2)) > 0
            [b_temp,~,~,~,~] = regress( cgrowth_temp, [ones(length(cgrowth_temp),1), htm_temp(1:end-2)]);
            chtm_reg_indiv_tr(iter) = b_temp(2);
            
            [b_temp,~,~,~,~] = regress( ygrowth_temp, [ones(length(cgrowth_temp),1), htm_temp(1:end-2)]);
            yhtm_reg_indiv_tr(iter) = b_temp(2);
            frac_htm_status_tr(ind_start(j):ind_end(j),i) = mean(htm_temp);
            
        end
        
        if  life_length(iter)> 10 &&  sum(htm_temp) > 2
            frac_htm_status_tr(ind_start(j):ind_end(j),i) = mean(htm_temp);
        end
        
        
        %{
        %%%%% abar agent-by-agent
        if life_length(iter) > 20
            atemp = a_sims_br(ind_start(j):ind_end(j),i);
            [rho_a,~,~,~,~] = regress( atemp(2:end), [ones(length(atemp(2:end)),1), atemp(1:end-1)]);
            abar_br_sims(i) = rho_a(1)/(1 - rho_a(2));
            rhoa_br_sims(i) = rho_a(2);
            
            atemp = a_sims_re(ind_start(j):ind_end(j),i);
            [rho_a,~,~,~,~] = regress( atemp(2:end), [ones(length(atemp(2:end)),1), atemp(1:end-1)]);
            abar_re_sims(i) = rho_a(1)/(1 - rho_a(2));
            rhoa_re_sims(i) = rho_a(2);
            
        end
        %}
    end
    
end


frac_htm_status_br    = frac_htm_status_br(sample_half:end,:);
frac_htm_br_panel = frac_htm_status_br(1:end-2,:);


frac_htm_status_re    = frac_htm_status_re(sample_half:end,:);
frac_htm_re_panel = frac_htm_status_re(1:end-2,:);

frac_htm_status_tr    = frac_htm_status_tr(sample_half:end,:);
frac_htm_tr_panel = frac_htm_status_tr(1:end-2,:);

temp =  mean( htm_sims_re(1:end-2,:));
temp =  mean( a_sims_re(1:end-2,:) <= w_re/6);
%temp(temp < 2/nsim ) = NaN; 
frac_htm_re_panel    = repmat(temp, [size(htm_sims_re(1:end-2,:),1),1]);


temp =  mean( htm_sims_tr(1:end-2,:));
temp =  mean( a_sims_tr(1:end-2,:) <= w_tr/6);
%temp(temp < 2/nsim ) = NaN; 
frac_htm_tr_panel    = repmat(temp, [size(htm_sims_tr(1:end-2,:),1),1]);


%temp =  mean( htm_sims_tr(1:end-2,:));
%frac_htm_tr_panel    = repmat(temp, [size(htm_sims_tr(1:end-2,:),1),1]);

[yhtm_reg_br2, ~, ~,~,~] = regress( ygrowth_sims_br(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:), frac_htm_br_panel(:)]);
[yhtm_reg_re2, ~, ~,~,~] = regress( ygrowth_sims_re(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:), frac_htm_re_panel(:)]);
[yhtm_reg_tr2, ~, ~,~,~] = regress( ygrowth_sims_tr(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:), frac_htm_tr_panel(:)]);

[chtm_reg_br2, ~, ~,~,~] = regress( cgrowth_sims_br(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:), frac_htm_br_panel(:)]);
[chtm_reg_re2, int_re, ~,~,~] = regress( cgrowth_sims_re(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:), frac_htm_re_panel(:)]);
[chtm_reg_tr2, ~, ~,~,~] = regress( cgrowth_sims_tr(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:), frac_htm_tr_panel(:)]);

[apc_br2, ~, ~,~,~] = regress( apc_sims_br_panel(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:), frac_htm_br_panel(:)]);
[apc_re2, ~, ~,~,~] = regress( apc_sims_re_panel(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:), frac_htm_re_panel(:)]);
[apc_tr2, ~, ~,~,~] = regress( apc_sims_tr_panel(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:), frac_htm_tr_panel(:)]);


[mpc_reg_br2, ~, ~,~,~] = regress( mpc_sims_br_panel(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:), frac_htm_br_panel(:)]);
[mpc_reg_re2, ~, ~,~,~] = regress( mpc_sims_re_panel(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:), frac_htm_re_panel(:)]);
[mpc_reg_tr2, ~, ~,~,~] = regress( mpc_sims_tr_panel(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:), frac_htm_tr_panel(:)]);

%%%%% MPCy regressions by Aguiar 
%%
avg_apc_sims = NaN(sim_periods,nsim);
ma_apc_sims = NaN(sim_periods,nsim);
avg_apclag_sims = NaN(sim_periods,nsim);

avg_apc_sims2 = NaN(sim_periods,nsim);
avg_totinc_sims = NaN(sim_periods,nsim);

ma_income_br  = NaN(sim_periods,nsim);
ma_chat_br  = NaN(sim_periods,nsim);
ma_cstar_br  = NaN(sim_periods,nsim);
adiff_br     = NaN(sim_periods, nsim); 

avg_income_br  = NaN(sim_periods,nsim);
avg_chat_br  = NaN(sim_periods,nsim);
avg_cstar_br  = NaN(sim_periods,nsim);


savwealth_sims_br = NaN(sim_periods, nsim); 
savwealth_sims_re = NaN(sim_periods, nsim); 

window = 2; 
window_dynan = 1; 

avg_coh_sims_br = NaN(sim_periods,nsim); 
avg_coh_sims_re = NaN(sim_periods,nsim); 
ma_coh_sims_br  = NaN(sim_periods,nsim); 
ma_coh_sims_re  = NaN(sim_periods,nsim); 

window_avgcoh = 4; 

for i = 1:nsim
    
    for j = sample_half:sim_periods
        
        ind_start = find(start_period(1:j,i),1,'last');
        avg_apc_sims(j,i) = sum( chat_sims_br(ind_start:j,i))/sum( rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i));
        %avg_apc_sims_re(j,i) = sum( chat_sims_br(ind_start:j,i))/sum( rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i));
        ma_apc_sims(j,i) = sum( chat_sims_br(max(ind_start, j - window+1):j,i))/sum( rt_br*a_sims_br(max(ind_start, j - window+1):j,i) + w_br*yi(max(ind_start, j - window+1):j,i));
        
        ma_income_br(j,i) = mean( w_br*yi(max(ind_start, j - window_dynan+1):j,i) + rt_br*a_sims_br(max(ind_start, j - window_dynan+1):j,i));
        ma_chat_br(j,i)   = mean(chat_sims_br(max(ind_start, j - window_dynan+1):j,i)  );
        ma_cstar_br(j,i)  = mean( min(cstar_brsim(  w_br*yi(max(ind_start, j - window_dynan+1):j,i) + (1+rt_br)*a_sims_br(max(ind_start, j - window_dynan+1):j,i)), w_br*yi(max(ind_start, j - window_dynan+1):j,i) + (1+rt_br)*a_sims_br(max(ind_start, j - window_dynan+1):j,i) -b)  );
        adiff_br(j,i)     = a_sims_br(j,i) - a_sims_br(max(ind_start, j - window_dynan),i);
        
        if age_sims_br(j,i) == 1
            adiff_br(j,i) = NaN; 
        end
        
        avg_income_br(j,i) = mean( w_br*yi(ind_start:j,i) + rt_br*a_sims_br(ind_start:j,i));
        avg_chat_br(j,i)   = mean(chat_sims_br(ind_start:j,i)  );
        avg_cstar_br(j,i)  = mean(cstar_brsim(  w_br*yi(ind_start:j,i) + (1+rt_br)*a_sims_br(ind_start:j,i))  );
        
        if j > ind_start
            avg_apclag_sims(j,i) = sum( chat_sims_br(ind_start:j-1,i))/sum( rt_br*a_sims_br(ind_start:j-1,i) + w_br*yi(ind_start:j-1,i));
        end
        
        avg_apc_sims2(j,i) = mean( chat_sims_br(ind_start:j,i)./( rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i)));
        
        avg_totinc_sims(j,i) = mean( rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i));
        
        
        savwealth_sims_br(j,i) = ( w_br*yi(j,i) + rt_br*a_sims_br(j,i) - chat_sims_br(j,i))./a_sims_br(j,i); 
        savwealth_sims_re(j,i) = ( w_re*yi(j,i) + rt_re*a_sims_re(j,i) - cstar_sims_re(j,i))./a_sims_re(j,i); 
        
        if ind_start <= j - window_avgcoh +1
            avg_coh_sims_br(j,i) = mean( w_br*yi(ind_start:j,i) + (1+rt_br)*a_sims_br(ind_start:j,i))/w_br; 
            ma_coh_sims_br(j,i) = mean( w_br*yi(max(ind_start,j-window_avgcoh+1):j,i) + (1+rt_br)*a_sims_br(max(ind_start,j-window_avgcoh+1):j,i))/w_br; 
            avg_coh_sims_re(j,i) = mean( w_re*yi(ind_start:j,i) + (1+rt_re)*a_sims_re(ind_start:j,i))/w_re; 
            ma_coh_sims_re(j,i) = mean( w_re*yi(max(ind_start,j-window_avgcoh+1):j,i) + (1+rt_re)*a_sims_re(max(ind_start,j-window_avgcoh+1):j,i))/w_re; 
        end

    end
    
end


%%%%%%%%% APC regressions 

apc_sims_br_panel = apc_sims_br(3:end,:);
%apc_sims_br_panel = avg_apc_sims(sample_half+2:end,:);
htm_sims_br_panel = htm_sims_br(1:end-2,:);
htm_sims_br_panel = htm_sims_br_panel(:);

apc_sims_re_panel = apc_sims_re(3:end,:);
%apc_sims_re_panel = apc_sims_re_panel(:);
htm_sims_re_panel = htm_sims_re(1:end-2,:);
htm_sims_re_panel = htm_sims_re_panel(:);

apc_sims_tr_panel = apc_sims_tr(3:end,:);
%apc_sims_tr_panel = apc_sims_tr_panel(:);
htm_sims_tr_panel = htm_sims_tr(1:end-2,:);
htm_sims_tr_panel = htm_sims_tr_panel(:);



[apc_br, bint, ~,~,~] = regress( apc_sims_br_panel(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:)]);
[apc_re, ~, ~,~,~] = regress( apc_sims_re_panel(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:)]);
[apc_tr, ~, ~,~,~] = regress( apc_sims_tr_panel(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:)]);

[apc_br2, ~, ~,~,~] = regress( apc_sims_br_panel(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:), frac_htm_br_panel(:)]);
[apc_re2, ~, ~,~,~] = regress( apc_sims_re_panel(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:), frac_htm_re_panel(:)]);
[apc_tr2, ~, ~,~,~] = regress( apc_sims_tr_panel(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:), frac_htm_tr_panel(:)]);

avg_income = (w_br*yi(sample_half+2:end,:) + rt_br*a_sims_br(sample_half+2:end,:) + w_br*yi(sample_half:end-2,:) + rt_br*a_sims_br(sample_half:end-2,:))/2; 
avg_income( age_sims_br(sample_half+2:end,:) <=2) = NaN; 

deltac_norm_br = ((chat_sims_br(sample_half+2:end,:) - chat_sims_br(sample_half:end-2,:))./avg_income)/2; 
deltay_norm_br = (w_br*(yi(sample_half+2:end,:) - yi(sample_half:end-2,:))./avg_income)/2;  

deltac_norm_br( age_sims_br(sample_half+2:end,:) <=2) = NaN; 
deltay_norm_br( age_sims_br(sample_half+2:end,:) <=2) = NaN; 

avg_income_re = (w_re*yi(sample_half+2:end,:) + rt_re*a_sims_re(sample_half+2:end,:) + w_re*yi(sample_half:end-2,:) + rt_re*a_sims_re(sample_half:end-2,:))/2; 
avg_income_re( age_sims_re(sample_half+2:end,:) <=2) = NaN; 

deltac_norm_re = ((cstar_sims_re(sample_half+2:end,:) - cstar_sims_re(sample_half:end-2,:))./avg_income_re)/2; 
deltay_norm_re = (w_re*(yi(sample_half+2:end,:) - yi(sample_half:end-2,:))./avg_income_re)/2;  

deltac_norm_re( age_sims_re(sample_half+2:end,:) <=2) = NaN; 
deltay_norm_re( age_sims_re(sample_half+2:end,:) <=2) = NaN; 

[mpcy_br, ~, ~,~,~] = regress( deltac_norm_br(:), [ones(length(deltac_norm_br(:)),1), deltay_norm_br(:), deltay_norm_br(:).*htm_sims_br_panel(:)]);
[mpcy_br2, ~, ~,~,~] = regress( deltac_norm_br(:), [ones(length(deltac_norm_br(:)),1), deltay_norm_br(:), deltay_norm_br(:).*htm_sims_br_panel(:), deltay_norm_br(:).*frac_htm_br_panel(:)]);

[mpcy_re, ~, ~,~,~] = regress( deltac_norm_re(:), [ones(length(deltac_norm_re(:)),1), deltay_norm_re(:), deltay_norm_re(:).*htm_sims_re_panel(:)]);
[mpcy_re2, ~, ~,~,~] = regress( deltac_norm_re(:), [ones(length(deltac_norm_re(:)),1), deltay_norm_re(:), deltay_norm_re(:).*htm_sims_re_panel(:), deltay_norm_re(:).*frac_htm_re_panel(:)]);

[mpc_reg_br2, ~, ~,~,~] = regress( mpc_sims_br_panel(:), [ones(length(apc_sims_br_panel(:)),1), htm_sims_br_panel(:), frac_htm_br_panel(:)]);
[mpc_reg_re2, ~, ~,~,~] = regress( mpc_sims_re_panel(:), [ones(length(apc_sims_re_panel(:)),1), htm_sims_re_panel(:), frac_htm_re_panel(:)]);
[mpc_reg_tr2, ~, ~,~,~] = regress( mpc_sims_tr_panel(:), [ones(length(apc_sims_tr_panel(:)),1), htm_sims_tr_panel(:), frac_htm_tr_panel(:)]);


mpc_temp =  mpc_br(sample_half+1:end,:);
%%%%% Winsorising
mpc_prc1 = prctile( mpc_temp(:), 1.5);
mpc_prc99 = prctile( mpc_temp(:), 98.5);
%mpc_temp( mpc_temp(:) <= mpc_prc1) = NaN;
%mpc_temp( mpc_temp(:) >= mpc_prc99) = NaN;

mpc_lag_temp = mpc_br(sample_half:end-1,:);
mpc_prc1 = prctile( mpc_temp(:), 1.5);
mpc_prc99 = prctile( mpc_temp(:), 98.5);
%mpc_lag_temp( mpc_lag_temp(:) <= mpc_prc1) = NaN;
%mpc_lag_temp( mpc_lag_temp(:) >= mpc_prc99) = NaN;

%mpc_temp2 =  (mpc_br2(sample_half+1:end,:) + (-mpc_br(sample_half+1:end,:)))/2;
mpc_temp2 =  mpc_br2(sample_half+1:end,:);
%%%%% Winsorising
%mpc_prc1 = prctile( mpc_temp2(:), 1.5);
%mpc_prc99 = prctile( mpc_temp2(:), 98.5);
%mpc_temp2( mpc_temp2(:) <= mpc_prc1) = NaN;
%mpc_temp2( mpc_temp2(:) >= mpc_prc99) = NaN;

mpc_lag_temp2 = mpc_br2(sample_half:end-1,:);
mpc_prc1 = prctile( mpc_temp2(:), 1.5);
mpc_prc99 = prctile( mpc_temp2(:), 98.5);
%mpc_lag_temp2( mpc_lag_temp2(:) <= mpc_prc1) = NaN;
%mpc_lag_temp2( mpc_lag_temp2(:) >= mpc_prc99) = NaN;

mpc_temp3 =  mpc_br3(sample_half+1:end,:);
%%%%% Winsorising
mpc_prc1 = prctile( mpc_temp3(:), 1.5);
mpc_prc99 = prctile( mpc_temp3(:), 98.5);
%mpc_temp3( mpc_temp3(:) <= mpc_prc1) = NaN;
%mpc_temp3( mpc_temp3(:) >= mpc_prc99) = NaN;

mpc_lag_temp3 = mpc_br3(sample_half:end-1,:);
mpc_prc1 = prctile( mpc_temp3(:), 1.5);
mpc_prc99 = prctile( mpc_temp3(:), 98.5);
%mpc_lag_temp3( mpc_lag_temp3(:) <= mpc_prc1) = NaN;
%mpc_lag_temp3( mpc_lag_temp3(:) >= mpc_prc99) = NaN;

age_temp = age_sims_br(sample_half+1:end,:);
fin_income = rt_br*a_sims_br(sample_half:end,:);
lab_income = w_br*yi(sample_half:end,:);
lagfin_income = fin_income(1:end-1,:);
lagfin_income(age_temp == 1) = NaN;
laglab_income = lab_income(1:end-1,:);
laglab_income(age_temp == 1) = NaN;

fin_income = fin_income(2:end,:);
lab_income = lab_income(2:end,:);

apc_temp = apc_sims_br(2:end,:);
apc_temp_lag = apc_sims_br(1:end-1,:);
apc_temp_lag( age_temp == 1) = NaN;
avg_apc_temp = avg_apc_sims(sample_half+1:end,:);
avg_apclag_temp = avg_apc_sims(sample_half:end-1,:);
avg_apclag_temp( age_temp == 1) = NaN;
avg_apclag_alt_temp = avg_apclag_sims(sample_half+1:end,:);
avg_apc_temp2 = avg_apc_sims2(sample_half+1:end,:);
avg_apclag_temp2 = avg_apc_sims2(sample_half:end-1,:);
avg_apclag_temp2( age_temp == 1) = NaN;

a_temp   = a_sims_br(sample_half+1:end,:);
a_temp_lag = a_sims_br(sample_half:end-1,:);
a_temp_lag( age_temp == 1) = NaN;

c_temp     = chat_sims_br(sample_half+1:end,:);
c_temp_lag = chat_sims_br(sample_half:end-1,:);
c_temp_lag( age_temp == 1) = NaN;

ya_temp = a_temp*(1+rt_br) + w_br*yi(sample_half+1:end,:);
crw_temp = rw_pol_interp(ya_temp);
ya_temp_lag = a_temp_lag*(1+rt_br) + w_br*yi(sample_half:end-1,:);
crw_temp_lag = rw_pol_interp(ya_temp_lag);
crw_temp_lag( age_temp == 1) = NaN;

crwa_temp = crw_temp + w_br/(1+rt_br)*(yi(sample_half+1:end,:) - 1);
crwa_temp_lag = crw_temp_lag + w_br/(1+rt_br)*(yi(sample_half:end-1,:) - 1);
crwa_temp_lag( age_temp == 1) = NaN;

avg_totinc_temp = avg_totinc_sims(sample_half:end,:);
logavg_totinc_temp = log(avg_totinc_sims(sample_half+1:end,:));
laglogavg_totinc_temp = log(avg_totinc_sims(sample_half:end-1,:));
laglogavg_totinc_temp( age_temp == 1) = NaN;


mpc_alt_temp = mpc_temp;
mpc_alt_temp(1,:) = NaN;
mpc_alt_temp(2:end,:) = diff( c_temp)./diff(w_br*yi(sample_half+1:end,:));
mpc_alt_temp(age_temp == 1) = mpc_temp(age_temp == 1);
mpc_prc1 = prctile( mpc_alt_temp(:), 1.5);
mpc_prc99 = prctile( mpc_alt_temp(:), 98.5);
mpc_alt_temp( mpc_alt_temp(:) <= mpc_prc1) = NaN;
mpc_alt_temp( mpc_alt_temp(:) >= mpc_prc99) = NaN;


alpha_temp = alpha_star_sims(sample_half+1:end,:);

logfin_income  = log(fin_income);
logfin_income(fin_income == 0) = 0;
loglab_income = log(lab_income);
laglogfin_income = log(lagfin_income);
laglogfin_income( lagfin_income == 0) = 0;
lagloglab_income = log(laglab_income);

logtot_income = log( fin_income + lab_income);
logtot_income_lag = log( lagfin_income + laglab_income);

unconstr_ind = htm_sims_br(2:end,:) == 0;
%unconstr_ind = a_sims_br(sample_half+1:end,:) > median( a_temp(:));
%unconstr_ind = (htm_sims_br(2:end,:) == 0).*(a_sims_br(sample_half+1:end,:) < prctile( a_temp(:) , 33));
%unconstr_ind = (a_sims_br(sample_half+1:end,:) < prctile( a_temp(:) , 33));

%%%%%%%%%%%

[multreg_cur, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), avg_apc_temp(unconstr_ind == 1 ), logtot_income(unconstr_ind == 1)]);
[multreg_lag, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), avg_apclag_temp(unconstr_ind == 1 ), logtot_income_lag(unconstr_ind == 1)]);
[multreg_lag, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), avg_apclag_temp(unconstr_ind == 1 ), logtot_income_lag(unconstr_ind == 1) , avg_apclag_temp(unconstr_ind == 1 ) .* logtot_income_lag(unconstr_ind == 1)]);

[multreg_lag, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), avg_apc_temp(unconstr_ind == 1 ), laglogfin_income(unconstr_ind == 1), lagloglab_income(unconstr_ind == 1)]);
[multreg_lag, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), apc_temp(unconstr_ind == 1 ), logfin_income(unconstr_ind == 1), loglab_income(unconstr_ind == 1)]);

[multreg_lag, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), avg_apclag_temp(unconstr_ind == 1 ), laglogfin_income(unconstr_ind == 1)]);
[multreg_lag, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), avg_apclag_temp(unconstr_ind == 1 ), logfin_income(unconstr_ind == 1)]);

[multreg_cur, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), apc_temp(unconstr_ind == 1 ), logtot_income(unconstr_ind == 1)]);
[multreg_cur, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 ), [ones(length(mpc_temp( unconstr_ind == 1 )),1), apc_temp_lag(unconstr_ind == 1 ), logtot_income_lag(unconstr_ind == 1)]);

%%%% fixed effects or age effects?
%{
betas_ind = NaN(3, total_agents);
iter = 0;
for i = 1:nsim
    
    ind_start = find(start_period(:,i) == 1);
    ind_end   = [ind_start(2:end)-1;sim_periods];
    
    for j = 1:length(ind_start)
        
        if ind_start(j) < sample_half
            continue
        end
        
        if ind_end(j) - ind_start(j) < 10
            continue
        end
        
        iter = iter + 1;
        
        [multreg_cur, ~,~,~,~] = regress( mpc_br2(ind_start(j):ind_end(j),i), [ones(length(mpc_br2(ind_start(j):ind_end(j),i)),1), avg_apc_sims(ind_start(j):ind_end(j),i), log( a_sims_br(ind_start(j):ind_end(j),i)*(1+rt_br) + w_br*yi(ind_start(j):ind_end(j),i))]);
        betas_ind(2:3,iter) = multreg_cur(2:3);
    end
end
%}

[multreg_cur, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 & age_temp < 10 ), [ones(length(mpc_temp( unconstr_ind == 1 & age_temp < 10 )),1), apc_temp(unconstr_ind == 1 & age_temp < 10 ), logtot_income(unconstr_ind == 1 & age_temp < 10 )]);
[multreg_cur, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 & age_temp > 10 ), [ones(length(mpc_temp( unconstr_ind == 1 & age_temp > 10 )),1), apc_temp(unconstr_ind == 1 & age_temp > 10 ), logtot_income(unconstr_ind == 1 & age_temp > 10 )]);

[multreg_cur, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 & age_temp < 10 ), [ones(length(mpc_temp( unconstr_ind == 1 & age_temp < 10 )),1), apc_temp_lag(unconstr_ind == 1 & age_temp < 10 ), logtot_income_lag(unconstr_ind == 1 & age_temp < 10 )]);
[multreg_cur, mpclaur_int,~,~,stats] = regress( mpc_temp2( unconstr_ind == 1 & age_temp > 10 ), [ones(length(mpc_temp( unconstr_ind == 1 & age_temp > 10 )),1), apc_temp_lag(unconstr_ind == 1 & age_temp > 10 ), logtot_income_lag(unconstr_ind == 1 & age_temp > 10 )]);


%%%
[mpc_apc, mpc_apc_int,r,~,stats] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), apc_temp(unconstr_ind == 1)]);
[mpc_apclag, mpc_apclag_int,~,~,stats] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), apc_temp_lag(unconstr_ind == 1)]);
[mpc_apc_age1, mpc_apc_int_age1,~,~,stats] = regress( mpc_temp2(unconstr_ind == 1 & age_temp == 1 ), [ones(length(mpc_temp(unconstr_ind == 1 & age_temp == 1 )),1), apc_temp(unconstr_ind == 1 & age_temp == 1 )]);
[mpc_apclag_age1, mpc_apclag_int_age1,~,~,stats] = regress( mpc_temp2(unconstr_ind == 1 & age_temp == 2), [ones(length(mpc_temp(unconstr_ind == 1 & age_temp == 2)),1), apc_temp_lag(unconstr_ind == 1 & age_temp == 2)]);


[mpc_crw, mpc_crw_int,~,~,~] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), c_temp(unconstr_ind == 1) - crw_temp(unconstr_ind == 1)]);
[mpc_crwlag, mpc_crwlag_int,~,~,~] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), c_temp_lag(unconstr_ind == 1) -  crw_temp_lag(unconstr_ind == 1)]);
[mpc_crwa, mpc_crwa_int,~,~,~] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), c_temp(unconstr_ind == 1) - crwa_temp(unconstr_ind == 1)]);
[mpc_crwalag, mpc_crwalag_int,~,~,~] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), c_temp_lag(unconstr_ind == 1) - crwa_temp_lag(unconstr_ind == 1)]);

[mpc_avgapc, mpc_avgapc_int,r,~,stats] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), avg_apc_temp(unconstr_ind == 1)]);
[mpc_avgapclag, mpc_avgapclag_int,~,~,stats] = regress( mpc_temp2(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), avg_apclag_temp(unconstr_ind == 1)]);
%[mpc_avgapc, mpc_avgapc_int,~,~,stats] = regress( mpc_temp(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), avg_apc_temp2(unconstr_ind == 1)]);
%[mpc_avgapclag, mpc_avgapclag_int,~,~,stats] = regress( mpc_temp(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), avg_apclag_temp2(unconstr_ind == 1)]);

%[mpc_apclag, mpc_apclag_int,~,~,~] = regress( mpc_temp( unconstr_ind(:) == 1 & age_temp (:) < 10), [ones(length(mpc_temp(unconstr_ind(:) == 1 &  age_temp (:) < 10)),1), apc_temp_lag(unconstr_ind(:) == 1 &  age_temp (:) < 10)]);


disp(' ');
disp(' --------------------------------')
disp([' APC Regressions '])
disp(' --------------------------------')
disp(['                       '  ]);
disp( '                        Coeff           Int  ');
disp(['   APC                :' , sprintf('%1.4f   ', [mpc_apc(2), mpc_apc_int(2,:)])  ]);
disp(['   APC   lag          :' , sprintf('%1.4f   ', [mpc_apclag(2), mpc_apclag_int(2,:)])  ]);
disp(['   chat - c_RW        :' , sprintf('%1.4f   ', [mpc_crw(2), mpc_crw_int(2,:)])  ]);
disp(['   chat - c_RW (lag)  :' , sprintf('%1.4f   ', [mpc_crwlag(2), mpc_crwlag_int(2,:)])  ]);
disp(['   chat - c_RWa       :' , sprintf('%1.4f   ', [mpc_crwa(2), mpc_crwa_int(2,:)])  ]);
disp(['   chat - c_RWa (lag) :' , sprintf('%1.4f   ', [mpc_crwalag(2), mpc_crwalag_int(2,:)])  ]);
disp(['   avgAPC             :' , sprintf('%1.4f   ', [mpc_avgapc(2), mpc_avgapc_int(2,:)])  ]);
disp(['   avgAPC   lag       :' , sprintf('%1.4f   ', [mpc_avgapclag(2), mpc_avgapclag_int(2,:)])  ]);

scatter(mpc_temp2(unconstr_ind == 1), apc_temp(unconstr_ind == 1))
scatter(mpc_temp3(unconstr_ind == 1), apc_temp(unconstr_ind == 1))
scatter(mpc_temp2(unconstr_ind == 1), apc_temp_lag(unconstr_ind == 1))
scatter(mpc_temp2(unconstr_ind == 1), avg_apc_temp(unconstr_ind == 1))


scatter(mpc_temp(unconstr_ind == 1 & age_temp == 1), apc_temp(unconstr_ind == 1 & age_temp == 1))
scatter(mpc_temp2(unconstr_ind == 1 & age_temp == 1), apc_temp(unconstr_ind == 1 & age_temp == 1))

scatter(mpc_temp(unconstr_ind == 1 & age_temp == 50), apc_temp(unconstr_ind == 1 & age_temp == 50))
scatter(mpc_temp2(unconstr_ind == 1 & age_temp > 50), apc_temp(unconstr_ind == 1 & age_temp > 50))

scatter(mpc_temp(unconstr_ind == 1 & alpha_temp  > 0.2), apc_temp(unconstr_ind == 1 & alpha_temp > 0.2))
scatter(mpc_temp2(unconstr_ind == 1 & alpha_temp  > 0.2), apc_temp(unconstr_ind == 1 & alpha_temp > 0.2))

scatter(mpc_temp(unconstr_ind == 1 & age_temp == 1), mpc_temp2(unconstr_ind == 1 & age_temp == 1))



%%%

%a_temp = a_sims_br(sample_half+1:end,:);
%c_temp = chat_sims_br(sample_half+1:end,:);

%ya_temp = a_temp*(1+rt_br) + w_br*yi(sample_half+1:end,:);
%crw_temp = rw_pol_interp(ya_temp);
%{
indrich = a_temp  > median(a_temp(:));
ind_aboverw = c_temp > crw_temp;

ind_rich_aboverw = indrich.*ind_aboverw;
ind_rich_belowrw = indrich.*(ind_aboverw == 0);

apc_rwa_temp = chat_sims_br(sample_half:end-1,:) - rw_pol_interp( a_sims_br(sample_half:end-1,:)*(1+rt_br) + w_br*yi(sample_half:end-1,:));

[mean(mpc_temp(ind_rich_aboverw(:) == 1)),mean(mpc_temp(ind_rich_belowrw(:) == 1))]
[mean(apc_temp(ind_rich_aboverw(:) == 1)),mean(apc_temp(ind_rich_belowrw(:) == 1))]
[mean(alpha_temp(ind_rich_aboverw(:) == 1)),mean(alpha_temp(ind_rich_belowrw(:) == 1))]


[mpc_laur, mpclaur_int,~,~,~] = regress( mpc_temp(:), [ones(length(mpc_temp(:)),1), apc_temp(:)]);

[mpc_laur, mpclaur_int,~,~,~] = regress( mpc_temp(:), [ones(length(mpc_temp(:)),1), apc_rwa_temp(:)]);
[mpc_laur, mpclaur_int,~,~,~] = regress( mpc_temp(unconstr_ind == 1), [ones(length(mpc_temp(unconstr_ind == 1)),1), apc_rwa_temp(unconstr_ind == 1)]);


mpc_temp_re =  mpc_re(sample_half+1:end,:);
a_temp = a_sims_re(sample_half+1:end,:);
c_temp = cstar_sims_re(sample_half+1:end,:);
ya_temp = a_temp*(1+rt_re) + w_re*yi(sample_half+1:end,:);

crw_temp = rw_pol_interp(ya_temp);
indrich_re = a_temp  > median(a_temp(:));
ind_aboverw_re = c_temp > crw_temp;

ind_rich_aboverw_re = indrich_re.*ind_aboverw_re;
ind_rich_belowrw_re = indrich_re.*(ind_aboverw_re == 0);

[mean(mpc_temp_re(ind_rich_aboverw_re(:) == 1)),mean(mpc_temp_re(ind_rich_belowrw_re(:) == 1))]
%}

%%% Learning traps stats

abar_br_sims = NaN(total_agents, 1);
rhoa_br_sims = NaN(total_agents, 1);
abar_re_sims = NaN(total_agents, 1);
rhoa_re_sims = NaN(total_agents, 1);

iter = 0;

flag = 0;



for i = 1:nsim
    
    ind_start = find(start_period(:,i) == 1);
    ind_end   = [ind_start(2:end)-1;sim_periods];
    
    for j = 1:length(ind_start)
        
        if ind_start(j) < sample_half
            continue
        end
        
        iter = iter + 1;
        
        
        
        life_length(iter)  = age_sims_br(ind_end(j),i);
        
        %{
        if life_length(iter) > 150
            flag = 1;
            break
        end
        %}
        
        %%%%% abar agent-by-agent
        if life_length(iter) > 20
            atemp = a_sims_br(ind_start(j) + floor(life_length(iter)/2) :ind_end(j),i)*(1+r_br) + w_br*yi(ind_start(j) + floor(life_length(iter)/2) :ind_end(j),i);
            if sum(atemp > sigma_l*4) < floor(life_length(iter)/4)
                abar_br_sims(iter) = mean(atemp);
                rhoa_br_sims(iter) = 0;
            else
                
                [rho_a,~,~,~,~] = regress( atemp(2:end), [ones(length(atemp(2:end)),1), atemp(1:end-1)]);
                abar_br_sims(iter) = rho_a(1)/(1 - rho_a(2));
                rhoa_br_sims(iter) = rho_a(2);
            end
            
            if ~isfinite(abar_br_sims(iter))
                flag = 1;
                break
            end
            
            atemp = a_sims_re(ind_start(j) + floor(life_length(iter)/2) :ind_end(j),i)*(1+r_re) + w_re*yi(ind_start(j) + floor(life_length(iter)/2) :ind_end(j),i);
            if sum(atemp > sigma_l*4) < floor(life_length(iter)/4)
                abar_br_sims(iter) = mean(atemp);
                rhoa_br_sims(iter) = 0;
            else
                [rho_a,~,~,~,~] = regress( atemp(2:end), [ones(length(atemp(2:end)),1), atemp(1:end-1)]);
                abar_re_sims(iter) = rho_a(1)/(1 - rho_a(2));
                rhoa_re_sims(iter) = rho_a(2);
            end
            
        end
        
    end
    
    if flag == 1
        break
    end
    
    
end


plot(a_sims_br(ind_start(j):ind_end(j),i))
yyaxis right
plot( alpha_star_sims(ind_start(j):ind_end(j),i))


xi_br = linspace(0,0.15,2);
xi_br = [xi_br, linspace(0.15,max(wealth_dist_br),50)];
xi_br = unique(xi_br);


figure
h = histogram( abar_br_sims - w_br,xi_br,'EdgeColor','b','Normalization','probability','DisplayStyle','stairs');
abar_pdf_br = h.Values./sum(h.Values);

figure
h_re = histogram(abar_re_sims - w_re ,h.BinEdges,'EdgeColor','r','Normalization','probability','DisplayStyle','stairs');
abar_pdf_re = h_re.Values./sum(h_re.Values);


xi_br = h.BinEdges;
xi_br = mean([xi_br(1:end-1)',xi_br(2:end)'],2);

figure
%plot(xi_br, wealth_pdf_re, 'Color', [0.85, 0.325, 0.098],'LineWidth',2.5)
%hold on
%plot(xi_br , wealth_pdf_br, 'Color', [0, 0.447, 0.741],'LineWidth',2.5)
%hold off
plot(xi_br, [abar_pdf_br',wealth_pdf_br',abar_pdf_re',wealth_pdf_re'],'LineWidth',2.5)


%%

shat_cross = NaN(sim_periods, nsim);
cross_ind_sims = NaN(sim_periods, nsim);
stateGrid_cross = linspace(b,50,1001);
rw_pol_cross = (rt_br/(1+rt_br))*stateGrid_cross' + 1/(1+rt_br)*w_br;
%rw_pol_cross = min( [stateGrid_cross', rw_pol_cross],[],2);

rw_grid_cross_ind = find(stateGrid_cross > rw_pol_cross(1), 1,'first');

pol_sims_size = 50;
chat_pol_sims = NaN(length(rw_pol_cross), pol_sims_size, nsim); 
shat_pol_sims = NaN(length(rw_pol_cross), pol_sims_size, nsim); 

ya_movavg  = NaN(sim_periods, nsim); 

num_zerocross_sims = NaN(sim_periods, nsim); 

tic
for i = 1:nsim
    
    shat_cross_temp = NaN( sim_periods, 1);
    cross_ind_temp = NaN(sim_periods,1);
    
    ya_avg_temp = NaN(sim_periods,1); 
    
    for j  =  sample_half + 1:sim_periods
        
        ind_start = find(start_period(1:j,i),1,'last');
        %eta_temp =  eta_sims(ind_start :j-1,i);
        
        ind_temp = ind_start:j;
        ind_temp = ind_temp( sigma_etasq_sims(ind_start:j,i) < 1e10);
        
        eta_temp = eta_sims( ind_temp,i);
        %eta_temp = cstar_brsim(yi(ind_temp,i)*w_br + a_sims_br(ind_temp,i)*(1+r_br)) + eps_etai(ind_temp,i)*0.1*sigma_etasq_sims(1,1);
        [chat_pol,shat_pol] =  gp_update( stateGrid_cross' , yi(ind_temp,i)*w_br + a_sims_br(ind_temp,i)*(1+r_br), eta_temp, sigma_etasq_sims(ind_temp,i), cstar_brsim,sigma_c,psi, cov_fun);
        
        %subplot(1,2,1); plot(stateGrid_cross, [chat_pol, cstar_brsim(stateGrid_cross)']) 
        %hold on 
        %scatter(yi(ind_temp,i)*w_br + a_sims_br(ind_temp,i)*(1+r_br), eta_temp)
        %subplot(1,2,2); plot(stateGrid_cross, shat_pol)

        
        if j >= sim_periods - pol_sims_size + 1
            chat_pol_sims(:, j - sim_periods + pol_sims_size,i) = chat_pol; 
            shat_pol_sims(:, j - sim_periods + pol_sims_size,i) = shat_pol; 
        end
        
        pol_diff = rw_pol_cross - chat_pol; 
        num_zerocross_sims(j,i)  = length(find(diff(sign(pol_diff))));
        
        [~,ya_ind] = min( abs(yi(j,i)*w_br + a_sims_br(j,i)*(1+r_br) - stateGrid_cross));
        
        if chat_pol(ya_ind) < rw_pol_cross(ya_ind)
            
            temp =  chat_pol(ya_ind:end)  - rw_pol_cross(ya_ind:end);
            cross_ind = find(temp > 0, 1,'first')-1;
            
            %if isempty(cross_ind)
            %    error('blah')
            %end
            
            if ~isempty(cross_ind)
                cross_ind_temp(j) = cross_ind + ya_ind;
                
            else
                cross_ind_temp(j) = length(stateGrid_cross);
            end
        else
            temp =  chat_pol(1:ya_ind)  - rw_pol_cross(1:ya_ind);
            cross_ind = find(temp < 0, 1,'last');
            if ~isempty(cross_ind)
                cross_ind_temp(j) = cross_ind;
            else
                cross_ind_temp(j) = 1;
            end
            
            %if isempty(cross_ind)
            %    error('blah neg')
            %end
            
        end
        
        if cross_ind_temp(j) < rw_grid_cross_ind
            cross_ind_temp(j) = find( chat_pol < stateGrid_cross',1,'first');
        end
        shat_cross_temp(j) = shat_pol(cross_ind_temp(j));
        
        ya_avg_temp(j) = mean( yi(ind_start:j-1,i)*w_br + a_sims_br(ind_start:j-1,i)*(1+r_br));
        
    end
    
    shat_cross(:,i) = shat_cross_temp;
    cross_ind_sims(:,i) = cross_ind_temp;
    ya_movavg(:,i) = ya_avg_temp; 
    i;
end
toc



%cross_ind_sims( isnan(cross_ind_sims)) = length(stateGrid_cross);
ya_cross = NaN(sim_periods, nsim);
ya_cross(sample_half+1:end,:) = stateGrid_cross(cross_ind_sims(sample_half+1:end,:));
ya = yi*w_br + a_sims_br*(1+rt_br);
ya_end   = ya(end,:);

ya_re = yi*w_re + a_sims_re*(1+rt_re); 

ya_dist_abs = abs( ya_cross(sample_half+1:end,:) - ya(sample_half+1:end,:));
ya_dist_abs_wholesample = abs( ya_cross - ya);
ya_dist = ya_cross(sample_half+1:end,:) - ya(sample_half+1:end,:) ;

yma_dist_abs = abs( ya_movavg(sample_half+1:end,:) - ya(sample_half+1:end,:));



%%

%W_cc = 1.79*W_cc*min(max( mu_t0, 1e-2),(1+r).*a_t + y_t - b).^(-W_cc - 1);

prior_sims_temp = prior_sims; 
prior_sims_temp( prior_sims < 1e-2) = 1e-2; 
prior_sims_temp( prior_sims > (1+rt_br)*a_sims_br + w_br*yi) = (1+rt_br)*a_sims_br( prior_sims > (1+rt_br)*a_sims_br + w_br*yi) + w_br*yi( prior_sims > (1+rt_br)*a_sims_br + w_br*yi);

cross_alpha0 = shat_cross <  kappa./(2*(1.79*prior_sims_temp).^(-gam-1));
cross_alpha0 = cross_alpha0(sample_half+1:end,:);

mpc_unconstr_crossa0 = mpc_temp2( unconstr_ind == 1 & cross_alpha0  == 0);
mpc_unconstr_crossa1 = mpc_temp2( unconstr_ind == 1 & cross_alpha0  == 1);


[nanmedian(mpc_unconstr_crossa0), nanmedian(mpc_unconstr_crossa1)]
[nanmean(mpc_unconstr_crossa0), nanmean(mpc_unconstr_crossa1)]

mpc_htm            = NaN(sim_periods - sample_half,2); 
mpc_aquint         = NaN(sim_periods - sample_half, 5); 
mpc_trap_htm       = NaN(sim_periods - sample_half,2); 
mpc_trap_aquint    = NaN(sim_periods - sample_half,5); 
mpc_notrap_htm     = NaN(sim_periods - sample_half,2); 
mpc_notrap_aquint  = NaN(sim_periods - sample_half,2); 

apc_trap_htm       = NaN(sim_periods - sample_half,2); 
apc_trap_aquint    = NaN(sim_periods - sample_half,5); 
apc_notrap_htm     = NaN(sim_periods - sample_half,2); 
apc_notrap_aquint  = NaN(sim_periods - sample_half,5); 
apc_aquint         = NaN(sim_periods - sample_half,5); 

avg_apc_trap_htm       = NaN(sim_periods - sample_half,2); 
avg_apc_trap_aquint    = NaN(sim_periods - sample_half,5); 
avg_apc_notrap_htm     = NaN(sim_periods - sample_half,2); 
avg_apc_notrap_aquint  = NaN(sim_periods - sample_half,5); 
avg_apc_aquint         = NaN(sim_periods - sample_half,5); 



chat_temp = chat_sims_br(sample_half+1:end,:); 
cstar_temp = min(cstar_brsim(ya), ya - b);
cstar_temp = cstar_temp(sample_half+1:end,:); 

crw_temp  = min(rw_pol_interp( ya ), ya - b); 
crw_temp = crw_temp(sample_half+1:end,:); 

chat_cstar_diff = NaN( sim_periods - sample_half,5); 
chat_cstar_diff_trap = NaN( sim_periods - sample_half,5); 
chat_cstar_diff_notrap = NaN( sim_periods - sample_half,5); 

chat_cstar_absdiff = NaN( sim_periods - sample_half,5); 
chat_cstar_absdiff_trap = NaN( sim_periods - sample_half,5); 
chat_cstar_absdiff_notrap = NaN( sim_periods - sample_half,5); 

chat_crw_diff = chat_cstar_diff; 
chat_crw_diff_trap = chat_cstar_diff; 
chat_crw_diff_notrap = chat_cstar_diff; 

chat_std_diff = chat_cstar_diff; 
chat_std_diff_trap = chat_cstar_diff; 
chat_std_diff_notrap = chat_cstar_diff; 

frac_intrap_aquint = NaN(sim_periods - sample_half, 5); 
frac_intrap_htm_aquint = NaN(sim_periods - sample_half, 5); 
frac_intrap_nothtm_aquint = NaN(sim_periods - sample_half, 5); 

ya_dist_aquint = NaN( sim_periods - sample_half,2); 
ya_dist_posfrac_aquint = NaN(sim_periods - sample_half, 2); 
ya_dist_trap_aquint = NaN( sim_periods - sample_half,2); 
ya_dist_posfrac_trap_aquint= NaN(sim_periods - sample_half, 2); 
ya_dist_notrap_aquint = NaN( sim_periods - sample_half,2); 
ya_dist_notrap_posfrac_aquint = NaN(sim_periods - sample_half, 2); 

apc_cstar_temp    = cstar_temp./( rt_br*a_sims_br(sample_half+1:end,:) + w_br*yi(sample_half+1:end,:));

avg_apc_cstar_temp = NaN(sim_periods, nsim); 
lifetime_earn_sims_br  = NaN(sim_periods, nsim); 
lifetime_income_sims_br = NaN(sim_periods, nsim); 
lifetime_earn_sims_re  = NaN(sim_periods, nsim); 
lifetime_income_sims_re = NaN(sim_periods, nsim); 

for i = 1:nsim
    
    for j = sample_half:sim_periods
        
        ind_start = find(start_period(1:j,i),1,'last');
        avg_apc_cstar_temp(j,i) = sum( min(cstar_brsim(ya(ind_start:j,i)),ya(ind_start:j,i) -b))/sum( rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i));
        
        disc_temp = (1+rt_br).^((ind_start:j) - ind_start);
        %disc_temp =1; 
        
        %lifetime_earn_br(j,i) = mean(w_br*yi(ind_start:j,i).*(disc_temp(end:-1:1)'));
        %lifetime_income_br(j,i) = mean((rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i)).*(disc_temp(end:-1:1)'));
        lifetime_earn_sims_br(j,i) = sum(w_br*yi(ind_start:j,i)./(disc_temp'));
        lifetime_earn_sims_br(j,i) =   sum(w_br*(yi(ind_start:j,i)-1) );
        %lifetime_income_br(j,i) = sum((rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i))./(disc_temp'));
        lifetime_income_sims_br(j,i) = a_sims_br(ind_start,i) + sum(( w_br*yi(ind_start:j,i))./(disc_temp'));
        %lifetime_income_br(j,i) = a_sims_br(ind_start,i).*disc_temp(end) + sum(( w_br*yi(ind_start:j,i)).*(disc_temp(end:-1:1)'));

        
        disc_temp = (1+rt_re).^((ind_start:j) - ind_start);
        %disc_temp =1; 

        %lifetime_earn_re(j,i) = mean(w_re*yi(ind_start:j,i).*(disc_temp(end:-1:1)'));
        %lifetime_income_re(j,i) = mean((rt_re*a_sims_re(ind_start:j,i) + w_re*yi(ind_start:j,i)).*(disc_temp(end:-1:1)'));
        lifetime_earn_sims_re(j,i) = sum(w_re*yi(ind_start:j,i)./(disc_temp'));
        lifetime_earn_sims_br(j,i) =   sum(w_re*(yi(ind_start:j,i)-1) );
        %lifetime_income_re(j,i) = sum((rt_re*a_sims_re(ind_start:j,i) + w_re*yi(ind_start:j,i))./(disc_temp'));
        lifetime_income_sims_re(j,i) = a_sims_re(ind_start,i) + sum(( w_re*yi(ind_start:j,i))./(disc_temp'));
        %lifetime_income_re(j,i) = a_sims_re(ind_start,i).*disc_temp(end) + sum( (w_re*yi(ind_start:j,i)).*(disc_temp(end:-1:1)'));


    end
    
end

[btemp,bint,~,~,~] = regress( a_sims_br(end,:)', [ones(nsim,1), lifetime_income_sims_br(end,:)']); 
[btemp,bint,~,~,~] = regress( a_sims_re(end,:)', [ones(nsim,1), lifetime_income_sims_re(end,:)']); 

avg_apc_cstar_temp = avg_apc_cstar_temp( sample_half+1:end,:);

apc_cstar_htm    = NaN(sim_periods - sample_half, 2); 
apc_cstar_aquint = NaN(sim_periods - sample_half, 2);  
avg_apc_cstar_htm    = NaN(sim_periods - sample_half, 2); 
avg_apc_cstar_aquint = NaN(sim_periods - sample_half, 2);  

%%%%%%%%%%% Dynan et. al savings rates analysis 

%%%Figure 1
quint_bins = 10; 

active_savings_medaquint_br = NaN(sim_periods - sample_half, quint_bins); 
netwealth_ch_medaquint_br   = NaN(sim_periods - sample_half, quint_bins); 
active_savings_cstar_medaquint_br = NaN(sim_periods - sample_half, quint_bins); 


active_savings_avgaquint_br = NaN(sim_periods - sample_half, quint_bins); 
netwealth_ch_avgaquint_br   = NaN(sim_periods - sample_half, quint_bins); 
active_savings_cstar_avgaquint_br = NaN(sim_periods - sample_half, quint_bins); 


active_savings_medaquint_br2 = NaN(sim_periods - sample_half, quint_bins); 
active_savings_cstar_medaquint_br2 = NaN(sim_periods - sample_half, quint_bins); 


active_savings_avgaquint_br2 = NaN(sim_periods - sample_half, quint_bins); 
active_savings_cstar_avgaquint_br2 = NaN(sim_periods - sample_half, quint_bins); 

actsav_sims_br = NaN(sim_periods, nsim); 
actsav_cstar_sims_br = NaN(sim_periods, nsim); 


actsav_medaquint_br = NaN(sim_periods - sample_half, quint_bins); 
actsav_avgaquint_br = NaN(sim_periods - sample_half, quint_bins); 
actsav_cstar_medaquint_br = NaN(sim_periods - sample_half, quint_bins); 
actsav_cstar_avgaquint_br = NaN(sim_periods - sample_half, quint_bins); 

savwealth_stdaquint_br = NaN(sim_periods - sample_half, quint_bins); 
savwealth_cstar_stdaquint_br = NaN(sim_periods - sample_half, quint_bins); 

actsav_stdaquint_br = NaN(sim_periods - sample_half, quint_bins); 
actsav_cstar_stdaquint_br = NaN(sim_periods - sample_half, quint_bins); 

for j =1:sim_periods
   
    if j >= sim_periods - size(chat_pol_sims,2)+1
       for i = 1:nsim 
            [~,ind_temp] = min( abs(  (1+rt_br)*a_sims_br(j  ,i)  + w_br - stateGrid_cross)); 

            actsav_sims_br(j,i) = ( (rt_br)*a_sims_br(j  ,i)  + w_br - min(chat_pol_sims(ind_temp,j - sim_periods + size(chat_pol_sims,2),i),(1+rt_br)*a_sims_br(j  ,i)  + w_br -b ))/((rt_br)*a_sims_br(j  ,i)  + w_br);
            actsav_cstar_sims_br(j,i) = ( (rt_br)*a_sims_br(j  ,i)  + w_br - min(cstar_brsim((1+rt_br)*a_sims_br(j  ,i)  + w_br),(1+rt_br)*a_sims_br(j  ,i)  + w_br -b ))/((rt_br)*a_sims_br(j  ,i)  + w_br);

            %savwealth_sims_br(j,i) = ( (rt_br)*a_sims_br(j  ,i)  + w_br - min(chat_pol_sims(ind_temp,j - sim_periods + size(chat_pol_sims,2),i),(1+rt_br)*a_sims_br(j  ,i)  + w_br -b ))/( a_sims_br(j  ,i)  );
            %savwealth_cstar_sims_br(j,i) = ( (rt_br)*a_sims_br(j  ,i)  + w_br - min(cstar_brsim((1+rt_br)*a_sims_br(j  ,i)  + w_br),(1+rt_br)*a_sims_br(j  ,i)  + w_br -b ))/( a_sims_br(j  ,i)  );

       end   
    end
        
end
    

cshare_aquint_br = NaN(sim_periods - sample_half, 5); 
cshare_aquint_re = NaN(sim_periods - sample_half, 5); 
expendrate_aquint_br = cshare_aquint_br; 
expendrate_aquint_re = cshare_aquint_br; 

mean_avgcoh_quint_br = NaN(sim_periods - sample_half, 5); 
mean_avgcoh_quint_re = NaN(sim_periods - sample_half, 5); 
median_avgcoh_quint_br = NaN(sim_periods - sample_half, 5); 
median_avgcoh_quint_re = NaN(sim_periods - sample_half, 5); 

cdelta_bigposs_aterc_br = NaN(sim_periods -sample_half, 3); 
cstardelta_bigposs_aterc_br = NaN(sim_periods -sample_half, 3); 
cdelta_bigposs_aterc_re = NaN(sim_periods -sample_half, 3); 
cdelta_bigposs_medaterc_br =  NaN(sim_periods -sample_half, 3); 

cdelta_bigposs_aterc_perc_br = NaN(sim_periods -sample_half, 3); 
cstardelta_bigposs_aterc_perc_br = NaN(sim_periods -sample_half, 3); 
cdelta_bigposs_aterc_perc_re = NaN(sim_periods -sample_half, 3); 


ya_bigposs_aterc_br = NaN(sim_periods -sample_half, 3); 
ya_bigposs_aterc_re = NaN(sim_periods -sample_half, 3); 

chatrwdiff_bigposs_aterc_br = NaN(sim_periods -sample_half, 3); 
cstarrwdiff_bigposs_aterc_br = NaN(sim_periods -sample_half, 3); 
cstarrwdiff_bigposs_aterc_re = NaN(sim_periods -sample_half, 3); 

adelta_bigposs_aterc_br     = NaN(sim_periods -sample_half, 3); 
ydelta_bigposs_aterc_br     = NaN(sim_periods -sample_half, 3); 

astardelta_bigposs_aterc_br     = NaN(sim_periods -sample_half, 3); 
ystardelta_bigposs_aterc_br     = NaN(sim_periods -sample_half, 3); 

adelta_bigposs_aterc_re     = NaN(sim_periods -sample_half, 3); 
ydelta_bigposs_aterc_re     = NaN(sim_periods -sample_half, 3); 

ygrowth_bigposs_aterc_br     = NaN(sim_periods -sample_half, 3); 


incdelta_bigposs_aterc_br = NaN(sim_periods - sample_half,3);
incdelta_bigposs_aterc_re = NaN(sim_periods - sample_half,3);
earndelta_bigposs_aterc_br = NaN(sim_periods - sample_half,3);
earndelta_bigposs_aterc_re = NaN(sim_periods - sample_half,3);

expydelta_aquint_br = NaN( sim_periods - sample_half, 5); 
expydelta_aquint_re = NaN( sim_periods - sample_half, 5); 
expydelta_aquint_br2 = NaN( sim_periods - sample_half, 5); 

constr_poss_br = NaN(sim_periods - sample_half, 3); 

cdelta_hist_br = []; 

scutoff = 1.27; %%% 1.16 (if we target 30% drop of avg income as in their model, or 1.325 for "on average" if we target 45% drop as in data, 1.43 for "poor"

for i = 1:sim_periods - sample_half
    
    htm_ind_temp = htm_sims_br(1+i,:) == 1;
    quint_temp = prctile( a_sims_br(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);
    quint_temp_re = prctile( a_sims_re(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);

    trap_ind_temp = cross_alpha0(i,:) == 1; 
    
    frac_intrap_aquint(i,1) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) <=quint_temp(1) ));
    frac_intrap_aquint(i,2) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   ) );
    frac_intrap_aquint(i,3) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   ) );
    frac_intrap_aquint(i,4) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   ) );
    frac_intrap_aquint(i,5) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(4) ) ); 
    
    frac_intrap_htm_aquint(i,1) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) <=quint_temp(1) & htm_ind_temp == 1));
    frac_intrap_htm_aquint(i,2) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)  & htm_ind_temp == 1  ) );
    frac_intrap_htm_aquint(i,3) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)  & htm_ind_temp == 1 ) );
    frac_intrap_htm_aquint(i,4) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)  & htm_ind_temp == 1 ) );
    frac_intrap_htm_aquint(i,5) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(4) & htm_ind_temp == 1)  ); 
    
    frac_intrap_nothtm_aquint(i,1) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) <=quint_temp(1) & htm_ind_temp == 0));
    frac_intrap_nothtm_aquint(i,2) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2) & htm_ind_temp == 0  ) );
    frac_intrap_nothtm_aquint(i,3) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3) & htm_ind_temp == 0  ) );
    frac_intrap_nothtm_aquint(i,4) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4) & htm_ind_temp == 0  ) );
    frac_intrap_nothtm_aquint(i,5) =  mean(trap_ind_temp( a_sims_br(i + sample_half,:) > quint_temp(4) & htm_ind_temp == 0) ); 
    
    mpc_htm(i,:) = [nanmean(mpc_temp2(i, htm_ind_temp == 1  )), nanmean(mpc_temp2(i,htm_ind_temp == 0  ))]; 
    mpc_aquint(i,1) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) <=quint_temp(1)  ));
    mpc_aquint(i,2) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)    ) );
    mpc_aquint(i,3) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)    ) );
    mpc_aquint(i,4) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)    ) );
    mpc_aquint(i,5) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(4)   ) ); 
    
    
    
    mpc_trap_htm(i,:) = [nanmean(mpc_temp2(i, htm_ind_temp == 1 & trap_ind_temp)), nanmean(mpc_temp2(i,htm_ind_temp == 0 & trap_ind_temp))]; 
    mpc_trap_aquint(i,1) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & trap_ind_temp));
    mpc_trap_aquint(i,2) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & trap_ind_temp ) );
    mpc_trap_aquint(i,3) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & trap_ind_temp ) );
    mpc_trap_aquint(i,4) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & trap_ind_temp ) );
    mpc_trap_aquint(i,5) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & trap_ind_temp ) ); 
    
    mpc_notrap_htm(i,:)    = [nanmean(mpc_temp2(i, htm_ind_temp == 1 & ~trap_ind_temp)), nanmean(mpc_temp2(i,htm_ind_temp == 0 & ~trap_ind_temp))]; 
    mpc_notrap_aquint(i,1) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & ~trap_ind_temp));
    mpc_notrap_aquint(i,2) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & ~trap_ind_temp ) );
    mpc_notrap_aquint(i,3) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & ~trap_ind_temp ) );
    mpc_notrap_aquint(i,4) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & ~trap_ind_temp ) );
    mpc_notrap_aquint(i,5) =  nanmean(mpc_temp2(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & ~trap_ind_temp ) ); 
    
    apc_trap_htm(i,:)    = [mean(apc_temp(i, htm_ind_temp == 1 & trap_ind_temp)), mean(apc_temp(i,htm_ind_temp == 0 & trap_ind_temp))]; 
    apc_trap_aquint(i,1) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & trap_ind_temp));
    apc_trap_aquint(i,2) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & trap_ind_temp ) );
    apc_trap_aquint(i,3) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & trap_ind_temp ) );
    apc_trap_aquint(i,4) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & trap_ind_temp ) );
    apc_trap_aquint(i,5) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & trap_ind_temp ) ); 
    
    apc_notrap_htm(i,:)    = [mean(apc_temp(i, htm_ind_temp == 1 & ~trap_ind_temp)), mean(apc_temp(i,htm_ind_temp == 0 & ~trap_ind_temp))]; 
    apc_notrap_aquint(i,1) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & ~trap_ind_temp));
    apc_notrap_aquint(i,2) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & ~trap_ind_temp ) );
    apc_notrap_aquint(i,3) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & ~trap_ind_temp ) );
    apc_notrap_aquint(i,4) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & ~trap_ind_temp ) );
    apc_notrap_aquint(i,5) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & ~trap_ind_temp ) ); 
    
    apc_aquint(i,1) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1)  ));
    apc_aquint(i,2) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)     ) );
    apc_aquint(i,3) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)     ) );
    apc_aquint(i,4) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)     ) );
    apc_aquint(i,5) =  mean(apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4)   ) ); 
    
    avg_apc_trap_htm(i,:)    = [mean(avg_apc_temp(i, htm_ind_temp == 1 & trap_ind_temp)), mean(avg_apc_temp(i,htm_ind_temp == 0 & trap_ind_temp))]; 
    avg_apc_trap_aquint(i,1) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & trap_ind_temp));
    avg_apc_trap_aquint(i,2) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & trap_ind_temp ) );
    avg_apc_trap_aquint(i,3) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & trap_ind_temp ) );
    avg_apc_trap_aquint(i,4) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & trap_ind_temp ) );
    avg_apc_trap_aquint(i,5) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & trap_ind_temp ) ); 
    
    avg_apc_notrap_htm(i,:)    = [mean(avg_apc_temp(i, htm_ind_temp == 1 & ~trap_ind_temp)), mean(avg_apc_temp(i,htm_ind_temp == 0 & ~trap_ind_temp))]; 
    avg_apc_notrap_aquint(i,1) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & ~trap_ind_temp));
    avg_apc_notrap_aquint(i,2) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & ~trap_ind_temp ) );
    avg_apc_notrap_aquint(i,3) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & ~trap_ind_temp ) );
    avg_apc_notrap_aquint(i,4) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & ~trap_ind_temp ) );
    avg_apc_notrap_aquint(i,5) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & ~trap_ind_temp ) ); 
    
    avg_apc_aquint(i,1) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1)  ));
    avg_apc_aquint(i,2) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)     ) );
    avg_apc_aquint(i,3) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)     ) );
    avg_apc_aquint(i,4) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)     ) );
    avg_apc_aquint(i,5) =  mean(avg_apc_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4)   ) ); 

    apc_cstar_htm(i,:)    = [mean(apc_cstar_temp(i, htm_ind_temp == 1 & trap_ind_temp)), mean(apc_cstar_temp(i,htm_ind_temp == 0 & trap_ind_temp))]; 
    apc_cstar_aquint(i,1) =  mean(apc_cstar_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1)  ));
    apc_cstar_aquint(i,2) =  mean(apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)    ) );
    apc_cstar_aquint(i,3) =  mean(apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   ) );
    apc_cstar_aquint(i,4) =  mean(apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)    ) );
    apc_cstar_aquint(i,5) =  mean(apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4)   ) ); 

    avg_apc_cstar_htm(i,:)    = [mean(avg_apc_cstar_temp(i, htm_ind_temp == 1 & trap_ind_temp)), mean(avg_apc_cstar_temp(i,htm_ind_temp == 0 & trap_ind_temp))]; 
    avg_apc_cstar_aquint(i,1) =  mean(avg_apc_cstar_temp(i  , a_sims_br(i + sample_half,:) <=quint_temp(1)  ));
    avg_apc_cstar_aquint(i,2) =  mean(avg_apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)    ) );
    avg_apc_cstar_aquint(i,3) =  mean(avg_apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   ) );
    avg_apc_cstar_aquint(i,4) =  mean(avg_apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)    ) );
    avg_apc_cstar_aquint(i,5) =  mean(avg_apc_cstar_temp(i  , a_sims_br(i + sample_half,:) > quint_temp(4)   ) ); 
    
    chat_cstar_diff(i,1)  = mean( log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) )));
    chat_cstar_diff(i,2)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)    )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2) )));
    chat_cstar_diff(i,3)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)    )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3) )));
    chat_cstar_diff(i,4)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)    )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4) )));
    chat_cstar_diff(i,5)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   ))                                                   - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   )));

    chat_cstar_diff_trap(i,1)  = mean( log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1)  & trap_ind_temp )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) & trap_ind_temp )));
    chat_cstar_diff_trap(i,2)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)  & trap_ind_temp)));
    chat_cstar_diff_trap(i,3)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)  & trap_ind_temp)));
    chat_cstar_diff_trap(i,4)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)  & trap_ind_temp)));
    chat_cstar_diff_trap(i,5)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   & trap_ind_temp ))                                                   - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   & trap_ind_temp )));

    chat_cstar_diff_notrap(i,1)  = mean( log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1)  & ~trap_ind_temp )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) & ~trap_ind_temp )));
    chat_cstar_diff_notrap(i,2)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & ~trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)  & ~trap_ind_temp)));
    chat_cstar_diff_notrap(i,3)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & ~trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)  & ~trap_ind_temp)));
    chat_cstar_diff_notrap(i,4)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & ~trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)  & ~trap_ind_temp)));
    chat_cstar_diff_notrap(i,5)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   & ~trap_ind_temp ))                                                   - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   & ~trap_ind_temp )));

    chat_cstar_absdiff(i,1)  = mean( abs( log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) ))));
    chat_cstar_absdiff(i,2)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)    )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2) ))));
    chat_cstar_absdiff(i,3)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)    )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3) ))));
    chat_cstar_absdiff(i,4)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)    )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4) ))));
    chat_cstar_absdiff(i,5)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   ))                                                   - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   ))));

    chat_cstar_absdiff_trap(i,1)  = mean( abs(log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1)  & trap_ind_temp )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) & trap_ind_temp ))));
    chat_cstar_absdiff_trap(i,2)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)  & trap_ind_temp))));
    chat_cstar_absdiff_trap(i,3)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)  & trap_ind_temp))));
    chat_cstar_absdiff_trap(i,4)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)  & trap_ind_temp))));
    chat_cstar_absdiff_trap(i,5)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   & trap_ind_temp ))                                                   - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   & trap_ind_temp ))));

    chat_cstar_absdiff_notrap(i,1)  = mean( abs(log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1)  & ~trap_ind_temp )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) & ~trap_ind_temp ))));
    chat_cstar_absdiff_notrap(i,2)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & ~trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)  & ~trap_ind_temp))));
    chat_cstar_absdiff_notrap(i,3)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & ~trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)  & ~trap_ind_temp))));
    chat_cstar_absdiff_notrap(i,4)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & ~trap_ind_temp  )) - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)  & ~trap_ind_temp))));
    chat_cstar_absdiff_notrap(i,5)  = mean( abs(log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   & ~trap_ind_temp ))                                                   - log(cstar_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   & ~trap_ind_temp ))));

    chat_crw_diff(i,1)  = mean( log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) )) - log(crw_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) )));
    chat_crw_diff(i,2)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)    )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2) )));
    chat_crw_diff(i,3)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)    )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3) )));
    chat_crw_diff(i,4)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)    )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4) )));
    chat_crw_diff(i,5)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   ))                                                   - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   )));

    chat_crw_diff_trap(i,1)  = mean( log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1)  & trap_ind_temp )) - log(crw_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) & trap_ind_temp )));
    chat_crw_diff_trap(i,2)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & trap_ind_temp  )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)  & trap_ind_temp)));
    chat_crw_diff_trap(i,3)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & trap_ind_temp  )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)  & trap_ind_temp)));
    chat_crw_diff_trap(i,4)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & trap_ind_temp  )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)  & trap_ind_temp)));
    chat_crw_diff_trap(i,5)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   & trap_ind_temp ))                                                   - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   & trap_ind_temp )));

    chat_crw_diff_notrap(i,1)  = mean( log(chat_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1)  & ~trap_ind_temp )) - log(crw_temp(i, a_sims_br(i + sample_half,:) <=quint_temp(1) & ~trap_ind_temp )));
    chat_crw_diff_notrap(i,2)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & ~trap_ind_temp  )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)  & ~trap_ind_temp)));
    chat_crw_diff_notrap(i,3)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & ~trap_ind_temp  )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)  & ~trap_ind_temp)));
    chat_crw_diff_notrap(i,4)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & ~trap_ind_temp  )) - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)  & ~trap_ind_temp)));
    chat_crw_diff_notrap(i,5)  = mean( log(chat_temp(i,  a_sims_br(i + sample_half,:) > quint_temp(4)   & ~trap_ind_temp ))                                                   - log(crw_temp(i, a_sims_br(i + sample_half,:) > quint_temp(4)   & ~trap_ind_temp )));

    
    ya_dist_trap_aquint(i,1) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & trap_ind_temp));
    ya_dist_trap_aquint(i,2) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & trap_ind_temp ) );
    ya_dist_trap_aquint(i,3) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & trap_ind_temp ) );
    ya_dist_trap_aquint(i,4) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & trap_ind_temp ) );
    ya_dist_trap_aquint(i,5) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & trap_ind_temp ) ); 
    
    ya_dist_notrap_aquint(i,1) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) <=quint_temp(1) & ~trap_ind_temp));
    ya_dist_notrap_aquint(i,2) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)   & ~trap_ind_temp ) );
    ya_dist_notrap_aquint(i,3) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)   & ~trap_ind_temp ) );
    ya_dist_notrap_aquint(i,4) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)   & ~trap_ind_temp ) );
    ya_dist_notrap_aquint(i,5) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(4) & ~trap_ind_temp ) ); 
    
    ya_dist_aquint(i,1) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) <=quint_temp(1)  ));
    ya_dist_aquint(i,2) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)     ) );
    ya_dist_aquint(i,3) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)     ) );
    ya_dist_aquint(i,4) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)     ) );
    ya_dist_aquint(i,5) =  median(ya_dist(i  , a_sims_br(i + sample_half,:) > quint_temp(4)   ) ); 
    
    
    active_savings_medaquint_br(i,1) = median( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - ma_chat_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    %active_savings_medaquint_br(i,3) = median( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - ma_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) ); 
    %active_savings_medaquint_br(i,4) = median( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - ma_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) ); 

    active_savings_avgaquint_br(i,1) = mean( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - ma_chat_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    %active_savings_avgaquint_br(i,3) = mean( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - ma_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) ); 
    %active_savings_avgaquint_br(i,4) = mean( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - ma_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) ); 
    
    netwealth_ch_medaquint_br(i,1) = median( (adiff_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))    )./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    netwealth_ch_medaquint_br(i,2) = median( (adiff_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)) )./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)) ); 
    netwealth_ch_medaquint_br(i,3) = median( (adiff_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) )./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) ); 
    netwealth_ch_medaquint_br(i,4) = median( (adiff_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) )./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) ); 
    netwealth_ch_medaquint_br(i,5) = median( (adiff_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(4))   )./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(4)) ); 

    netwealth_ch_avgaquint_br(i,1) = mean( (adiff_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))    )./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    netwealth_ch_avgaquint_br(i,2) = mean( (adiff_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)) )./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)) ); 
    netwealth_ch_avgaquint_br(i,3) = mean( (adiff_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) )./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) ); 
    netwealth_ch_avgaquint_br(i,4) = mean( (adiff_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) )./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) ); 
    netwealth_ch_avgaquint_br(i,5) = mean( (adiff_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(4))   )./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(4)) ); 

    
    cshare_aquint_br(i,1) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./sum(chat_sims_br(i+sample_half,:)); 
    cshare_aquint_br(i,2) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2)))./sum(chat_sims_br(i+sample_half,:)); 
    cshare_aquint_br(i,3) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3)))./sum(chat_sims_br(i+sample_half,:)); 
    cshare_aquint_br(i,4) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4)))./sum(chat_sims_br(i+sample_half,:)); 
    cshare_aquint_br(i,5) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(4)))./sum(chat_sims_br(i+sample_half,:)); 

    cshare_aquint_re(i,1) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) <=quint_temp_re(1)))./sum(cstar_sims_re(i+sample_half,:)); 
    cshare_aquint_re(i,2) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(1) & a_sims_re(i+sample_half,:) <=quint_temp_re(2)))./sum(cstar_sims_re(i+sample_half,:)); 
    cshare_aquint_re(i,3) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(2) & a_sims_re(i+sample_half,:) <=quint_temp_re(3)))./sum(cstar_sims_re(i+sample_half,:)); 
    cshare_aquint_re(i,4) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(3) & a_sims_re(i+sample_half,:) <=quint_temp_re(4)))./sum(cstar_sims_re(i+sample_half,:)); 
    cshare_aquint_re(i,5) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(4)))./sum(cstar_sims_re(i+sample_half,:)); 
    
    expendrate_aquint_br(i,1) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./sum(rt_br*a_sims_br(i+sample_half,a_sims_br(i+sample_half,:) <=quint_temp(1)) + w_br*yi(i+sample_half,a_sims_br(i+sample_half,:) <=quint_temp(1))); 
    expendrate_aquint_br(i,2) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2)))./sum(rt_br*a_sims_br(i+sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2)) + w_br*yi(i+sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2))); 
    expendrate_aquint_br(i,3) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3)))./sum(rt_br*a_sims_br(i+sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3)) + w_br*yi(i+sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3))); 
    expendrate_aquint_br(i,4) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4)))./sum(rt_br*a_sims_br(i+sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4)) + w_br*yi(i+sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4))); 
    expendrate_aquint_br(i,5) = sum( chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(4)))./sum(rt_br*a_sims_br(i+sample_half,a_sims_br(i+sample_half,:) > quint_temp(4)) + w_br*yi(i+sample_half,a_sims_br(i+sample_half,:) > quint_temp(4))); 

    
    expydelta_aquint_br(i,1) = mean(  rt_br*ya(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) + w_br - (1+rt_br)*chat_sims_br(i+sample_half,a_sims_br(i+sample_half,:) <=quint_temp(1))); 
    expydelta_aquint_br(i,2) = mean(  rt_br*ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2)) + w_br  - (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2))); 
    expydelta_aquint_br(i,3) = mean(  rt_br*ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3)) + w_br  - (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3))); 
    expydelta_aquint_br(i,4) = mean(  rt_br*ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4)) + w_br  - (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4)));
    expydelta_aquint_br(i,5) = mean(  rt_br*ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(4)  ) + w_br  - (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(4)  ));

    quint_one = a_sims_br(i+sample_half,:) <=quint_temp(1) & age_sims_br(i + sample_half, :) > 1;
    
    expydelta_aquint_br2(i,1) = mean(  (1+rt_br)*rw_pol_interp(ya(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1) & age_sims_br(i + sample_half, :) > 1))                                              -  (1+rt_br)*chat_sims_br(i+sample_half,a_sims_br(i+sample_half,:) <=quint_temp(1) & age_sims_br(i + sample_half, :) > 1)); 
    expydelta_aquint_br2(i,2) = mean(  (1+rt_br)*rw_pol_interp(ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2) & age_sims_br(i + sample_half, :) > 1)) -  (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(1) & a_sims_br(i+sample_half,:) <=quint_temp(2) & age_sims_br(i + sample_half, :) > 1)); 
    expydelta_aquint_br2(i,3) = mean(  (1+rt_br)*rw_pol_interp(ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3) & age_sims_br(i + sample_half, :) > 1)) -  (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(2) & a_sims_br(i+sample_half,:) <=quint_temp(3) & age_sims_br(i + sample_half, :) > 1)); 
    expydelta_aquint_br2(i,4) = mean(  (1+rt_br)*rw_pol_interp(ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4) & age_sims_br(i + sample_half, :) > 1)) -  (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(3) & a_sims_br(i+sample_half,:) <=quint_temp(4) & age_sims_br(i + sample_half, :) > 1));
    expydelta_aquint_br2(i,5) = mean(  (1+rt_br)*rw_pol_interp(ya(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(4) & age_sims_br(i + sample_half, :) > 1 ))                                             - (1+rt_br)*chat_sims_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(4)  & age_sims_br(i + sample_half, :) > 1));

    %expendrate_aquint_re(i,1) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) <=quint_temp_re(1)))./sum(cstar_sims_re(i+sample_half,:)); 
    %expendrate_aquint_re(i,2) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(1) & a_sims_re(i+sample_half,:) <=quint_temp_re(2)))./sum(cstar_sims_re(i+sample_half,:)); 
    %expendrate_aquint_re(i,3) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(2) & a_sims_re(i+sample_half,:) <=quint_temp_re(3)))./sum(cstar_sims_re(i+sample_half,:)); 
    %expendrate_aquint_re(i,4) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(3) & a_sims_re(i+sample_half,:) <=quint_temp_re(4)))./sum(cstar_sims_re(i+sample_half,:)); 
    %expendrate_aquint_re(i,5) = sum( cstar_sims_re(i + sample_half, a_sims_re(i+sample_half,:) > quint_temp_re(4)))./sum(cstar_sims_re(i+sample_half,:)); 
    
    
    quint_temp    = prctile( a_sims_br(i + sample_half,:), 100*linspace(1/quint_bins, 1-1/quint_bins,quint_bins-1));
    quint_temp_re = prctile( a_sims_re(i + sample_half,:), 100*linspace(1/quint_bins, 1-1/quint_bins,quint_bins-1));

    active_savings_cstar_medaquint_br(i,1) = median( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - ma_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    active_savings_cstar_avgaquint_br(i,1) = mean( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - ma_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    active_savings_medaquint_br2(i,1) = median( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - avg_chat_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./avg_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    active_savings_avgaquint_br2(i,1) = mean( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - avg_chat_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./avg_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    active_savings_cstar_medaquint_br2(i,1) = median( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - avg_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 
    active_savings_cstar_avgaquint_br2(i,1) = mean( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))  - avg_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)))./avg_income_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1)) ); 

    actsav_medaquint_br(i,1)       = median( actsav_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))); 
    actsav_avgaquint_br(i,1)       = mean( actsav_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))); 
    actsav_cstar_medaquint_br(i,1) = median( actsav_cstar_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))); 
    actsav_cstar_avgaquint_br(i,1) = mean( actsav_cstar_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))); 

    actsav_stdaquint_br(i,1)       = std( actsav_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))); 
    actsav_cstar_stdaquint_br(i,1) = std( actsav_cstar_sims_br(i + sample_half, a_sims_br(i+sample_half,:) <=quint_temp(1))); 

    for j = 1:length(quint_temp)-1
        
        active_savings_medaquint_br(i,j+1) = median( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - ma_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) ); 
        active_savings_avgaquint_br(i,j+1)   = mean( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - ma_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) ); 

        active_savings_cstar_medaquint_br(i,j+1) = median( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - ma_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) );
        %active_savings_cstar_medaquint_br(i,3) = median( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - ma_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) );
        %active_savings_cstar_medaquint_br(i,4) = median( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - ma_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) );
        
        active_savings_cstar_avgaquint_br(i,j+1) = mean( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - ma_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) );
        %active_savings_cstar_avgaquint_br(i,3) = mean( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - ma_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) );
        %active_savings_cstar_avgaquint_br(i,4) = mean( (ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - ma_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./ma_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) );
        
        active_savings_medaquint_br2(i,j+1) = median( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - avg_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) );
        %active_savings_medaquint_br2(i,3) = median( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - avg_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) );
        %active_savings_medaquint_br2(i,4) = median( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - avg_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) );
        
        active_savings_avgaquint_br2(i,j+1) = mean( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - avg_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) );
        %active_savings_avgaquint_br2(i,3) = mean( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - avg_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) );
        %active_savings_avgaquint_br2(i,4) = mean( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - avg_chat_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) );
        
        active_savings_cstar_medaquint_br2(i,j+1) = median( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - avg_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) );
        %active_savings_cstar_medaquint_br2(i,3) = median( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - avg_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) );
        %active_savings_cstar_medaquint_br2(i,4) = median( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - avg_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) );
        
        active_savings_cstar_avgaquint_br2(i,j+1) = mean( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))  - avg_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) );
        %active_savings_cstar_avgaquint_br2(i,3) = mean( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3))  - avg_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) );
        %active_savings_cstar_avgaquint_br2(i,4) = mean( (avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4))  - avg_cstar_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)))./avg_income_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) );
        
        actsav_medaquint_br(i,j+1)       = median( actsav_sims_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))) ; 
        actsav_avgaquint_br(i,j+1)       = mean( actsav_sims_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1)) ); 
        actsav_cstar_medaquint_br(i,j+1) = median( actsav_cstar_sims_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))); 
        actsav_cstar_avgaquint_br(i,j+1) = mean( actsav_cstar_sims_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))) ; 
        
        savwealth_stdaquint_br(i,j+1)       = std( savwealth_sims_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))) ; 
        savwealth_cstar_stdaquint_br(i,j+1) = std( savwealth_sims_re(i + sample_half, a_sims_re(i + sample_half,:) > quint_temp_re(j) &  a_sims_re(i + sample_half,:) <= quint_temp_re(j+1))); 
        
        actsav_stdaquint_br(i,j+1)       = std( actsav_sims_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))) ; 
        actsav_cstar_stdaquint_br(i,j+1) = std( actsav_cstar_sims_br(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(j) &  a_sims_br(i + sample_half,:) <= quint_temp(j+1))); 

    end
    
     active_savings_medaquint_br(i,end)          = median( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - ma_chat_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) ); 
     active_savings_avgaquint_br(i,end)          = mean( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - ma_chat_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) ); 
     active_savings_cstar_medaquint_br(i,end)  = median( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - ma_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) ); 
     active_savings_cstar_avgaquint_br(i,end)  = mean( (ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - ma_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) ); 
     active_savings_medaquint_br2(i,end)       = median( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - avg_chat_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./avg_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) ); 
     active_savings_avgaquint_br2(i,end)       = mean( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - avg_chat_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./avg_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) ); 
     active_savings_cstar_medaquint_br2(i,end) = median( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - avg_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./ma_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) );
     active_savings_cstar_avgaquint_br2(i,end) = mean( (avg_income_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))  - avg_cstar_br(i + sample_half, a_sims_br(i+sample_half,:) > quint_temp(end)))./avg_income_br(i + sample_half, a_sims_br(i+sample_half,:)  > quint_temp(end)) );
     
     actsav_medaquint_br(i,end)       = median( actsav_sims_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))) ;
     actsav_avgaquint_br(i,end)       = mean( actsav_sims_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end)));
     actsav_cstar_medaquint_br(i,end) = median( actsav_cstar_sims_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))) ;
     actsav_cstar_avgaquint_br(i,end) = mean( actsav_cstar_sims_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))) ;
     
     savwealth_stdaquint_br(i,end)       = std( savwealth_sims_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end)));
     savwealth_cstar_stdaquint_br(i,end) = std( savwealth_sims_re(i + sample_half, a_sims_re(i+sample_half,:)   > quint_temp_re(end))) ;
     actsav_stdaquint_br(i,end)          = std( actsav_sims_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end)));
     actsav_cstar_stdaquint_br(i,end)    = std( actsav_cstar_sims_br(i + sample_half, a_sims_br(i+sample_half,:)   > quint_temp(end))) ;
     
     quint_temp    = prctile( avg_coh_sims_br(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);
     quint_temp_re = prctile( avg_coh_sims_br(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);
     
     
    mean_avgcoh_quint_br(i,1) = mean( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:) <= quint_temp(1))); 
    mean_avgcoh_quint_br(i,2) = mean( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(1) & avg_coh_sims_br(i+sample_half,:) <= quint_temp(2))); 
    mean_avgcoh_quint_br(i,3) = mean( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(2) & avg_coh_sims_br(i+sample_half,:) <= quint_temp(3))); 
    mean_avgcoh_quint_br(i,4) = mean( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(3) & avg_coh_sims_br(i+sample_half,:) <= quint_temp(4))); 
    mean_avgcoh_quint_br(i,5) = mean( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(4))); 
    
    median_avgcoh_quint_br(i,1) = median( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:) <= quint_temp(1))); 
    median_avgcoh_quint_br(i,2) = median( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(1) & avg_coh_sims_br(i+sample_half,:) <= quint_temp(2))); 
    median_avgcoh_quint_br(i,3) = median( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(2) & avg_coh_sims_br(i+sample_half,:) <= quint_temp(3))); 
    median_avgcoh_quint_br(i,4) = median( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(3) & avg_coh_sims_br(i+sample_half,:) <= quint_temp(4))); 
    median_avgcoh_quint_br(i,5) = median( avg_coh_sims_br(i + sample_half, avg_coh_sims_br(i+sample_half,:)  > quint_temp(4))); 

    mean_avgcoh_quint_re(i,1) = mean( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(1))); 
    mean_avgcoh_quint_re(i,2) = mean( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(1) & avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(2))); 
    mean_avgcoh_quint_re(i,3) = mean( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(2) & avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(3))); 
    mean_avgcoh_quint_re(i,4) = mean( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(3) & avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(4))); 
    mean_avgcoh_quint_re(i,5) = mean( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(4))); 
    
    median_avgcoh_quint_re(i,1) = median( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(1))); 
    median_avgcoh_quint_re(i,2) = median( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(1) & avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(2))); 
    median_avgcoh_quint_re(i,3) = median( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(2) & avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(3))); 
    median_avgcoh_quint_re(i,4) = median( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(3) & avg_coh_sims_re(i+sample_half,:) <= quint_temp_re(4))); 
    median_avgcoh_quint_re(i,5) = median( avg_coh_sims_re(i + sample_half, avg_coh_sims_re(i+sample_half,:)  > quint_temp_re(4))); 
    
    terc_temp    = prctile(    a_sims_br(i + sample_half-1,:), 100*[0.333, 0.666]);
    terc_temp_re = prctile( a_sims_re(i + sample_half-1,:), 100*[0.333, 0.666]);

    temp_ind = yi(i + sample_half-1,:) > scutoff & age_sims_br(i + sample_half,:) ~= 1; 
    
    cdelta_bigposs_aterc_br(i + sample_half,1) = mean(chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    cdelta_bigposs_aterc_br(i + sample_half,2) = mean(chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    cdelta_bigposs_aterc_br(i + sample_half,3) = mean(chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    constr_poss_br(i + sample_half, 1) = mean((chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)))==0); 
    constr_poss_br(i + sample_half, 2) = mean((chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)))==0); 
    constr_poss_br(i + sample_half, 3) = mean((chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)))==0); 

    cdelta_hist_br = [chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)), cdelta_hist_br]; 
    
    cdelta_bigposs_aterc_perc_br(i + sample_half,1) = mean((chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1))))./mean(chat_sims_br(i+sample_half-2, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    cdelta_bigposs_aterc_perc_br(i + sample_half,2) = mean((chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))))./mean(chat_sims_br(i+sample_half-2, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    cdelta_bigposs_aterc_perc_br(i + sample_half,3) = mean((chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))))./mean(chat_sims_br(i+sample_half-2, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    
    cdelta_bigposs_medaterc_br(i + sample_half,1) = median(chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    cdelta_bigposs_medaterc_br(i + sample_half,2) = median(chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    cdelta_bigposs_medaterc_br(i + sample_half,3) = median(chat_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    ya_temp1 =  ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) -  (1+rt_br)*(cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1))) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1) ) )); 
    ya_temp2 =  ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - (1+rt_br)*(cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))));
    ya_temp3 =  ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - (1+rt_br)*(cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))));
    
    
    cstardelta_bigposs_aterc_br(i + sample_half,1) = mean(cstar_brsim(ya_temp1) - cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1))));
    cstardelta_bigposs_aterc_br(i + sample_half,2) = mean(cstar_brsim(ya_temp2) - cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))));
    cstardelta_bigposs_aterc_br(i + sample_half,3) = mean(cstar_brsim(ya_temp3) - cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))));
    
    cstardelta_bigposs_aterc_perc_br(i + sample_half,1) = mean((cstar_brsim(ya_temp1) - cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1))))./cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1))));
    cstardelta_bigposs_aterc_perc_br(i + sample_half,2) = mean((cstar_brsim(ya_temp2) - cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))))./cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))));
    cstardelta_bigposs_aterc_perc_br(i + sample_half,3) = mean((cstar_brsim(ya_temp3) - cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))))./cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))));
 
    %cstardelta_bigposs_aterc_perc_br(i + sample_half,1) = mean((log(cstar_brsim(ya_temp1)) - log(cstar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)))));
    %cstardelta_bigposs_aterc_perc_br(i + sample_half,2) = mean((log(cstar_brsim(ya_temp2)) - log(cstar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)))));
    %cstardelta_bigposs_aterc_perc_br(i + sample_half,3) = mean((log(cstar_brsim(ya_temp3)) - log(cstar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))))./mean(cstar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
 
    
    cdelta_bigposs_aterc_re(i + sample_half,1) = mean(cstar_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) - cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)));
    cdelta_bigposs_aterc_re(i + sample_half,2) = mean(cstar_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ) - cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ));
    cdelta_bigposs_aterc_re(i + sample_half,3) = mean(cstar_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) - cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)));
    
    cdelta_bigposs_aterc_perc_re(i + sample_half,1) = mean((cstar_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) - cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1))))./mean(cstar_sims_re(i+sample_half-2, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)));
    cdelta_bigposs_aterc_perc_re(i + sample_half,2) = mean((cstar_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2)) - cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2))))./mean(cstar_sims_re(i+sample_half-2, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2)));
    cdelta_bigposs_aterc_perc_re(i + sample_half,3) = mean((cstar_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) - cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2))))./mean(cstar_sims_re(i+sample_half-2, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)));
 
    
    ya_bigposs_aterc_br(i + sample_half,1) = mean(ya(i+sample_half - 1, temp_ind & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    ya_bigposs_aterc_br(i + sample_half,2) = mean(ya(i+sample_half - 1, temp_ind & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    ya_bigposs_aterc_br(i + sample_half,3) = mean(ya(i+sample_half - 1, temp_ind & a_sims_br(i+sample_half-1,:) > terc_temp(2)));

    %ygrowth_bigposs_aterc_br(i + sample_half, 1)     = NaN(sim_periods -sample_half, 3); 
    
    ygrowth_bigposs_aterc_br(i + sample_half,1) = mean(diff(ya(i+sample_half - 2 : i+sample_half-1, temp_ind & a_sims_br(i+sample_half-1,:) < terc_temp(1))));
    ygrowth_bigposs_aterc_br(i + sample_half,2) = mean(diff(ya(i+sample_half - 2 : i+sample_half-1 , temp_ind & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))));
    ygrowth_bigposs_aterc_br(i + sample_half,3) = mean(diff(ya(i+sample_half - 2 : i+sample_half-1, temp_ind & a_sims_br(i+sample_half-1,:) > terc_temp(2))));

    
    chatrwdiff_bigposs_aterc_br(i + sample_half,1) = mean(chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1) ) )); 
    chatrwdiff_bigposs_aterc_br(i + sample_half,2) = mean(chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))));
    chatrwdiff_bigposs_aterc_br(i + sample_half,3) = mean(chat_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))));
    
    cstarrwdiff_bigposs_aterc_br(i + sample_half,1) = mean(cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1))) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1) ) )); 
    cstarrwdiff_bigposs_aterc_br(i + sample_half,2) = mean(cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))));
    cstarrwdiff_bigposs_aterc_br(i + sample_half,3) = mean(cstar_brsim(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))) - rw_pol_interp(ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))));
    
    cstarrwdiff_bigposs_aterc_re(i + sample_half,1) = mean(cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) - rw_pol_interp_re(ya_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1))));
    cstarrwdiff_bigposs_aterc_re(i + sample_half,2) = mean(cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ) - rw_pol_interp_re(ya_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) )));
    cstarrwdiff_bigposs_aterc_re(i + sample_half,3) = mean(cstar_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) - rw_pol_interp_re(ya_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2))));
    
    adelta_bigposs_aterc_br(i + sample_half,1) = mean(a_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    adelta_bigposs_aterc_br(i + sample_half,2) = mean(a_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    adelta_bigposs_aterc_br(i + sample_half,3) = mean(a_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    astardelta_bigposs_aterc_br(i + sample_half,1) = mean(astar_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - astar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    astardelta_bigposs_aterc_br(i + sample_half,2) = mean(astar_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - astar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    astardelta_bigposs_aterc_br(i + sample_half,3) = mean(astar_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - astar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    adelta_bigposs_aterc_re(i + sample_half,1) = mean(a_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) - a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)));
    adelta_bigposs_aterc_re(i + sample_half,2) = mean(a_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ) - a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ));
    adelta_bigposs_aterc_re(i + sample_half,3) = mean(a_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) - a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)));
    
    ydelta_bigposs_aterc_br(i + sample_half,1) = mean(ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    ydelta_bigposs_aterc_br(i + sample_half,2) = mean(ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    ydelta_bigposs_aterc_br(i + sample_half,3) = mean(ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    %yshock_bigposs_aterc_br(i + sample_half,1) = mean(ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    %yshock_bigposs_aterc_br(i + sample_half,2) = mean(ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    %yshock_bigposs_aterc_br(i + sample_half,3) = mean(ya(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - ya(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));

    %ystardelta_bigposs_aterc_br(i + sample_half,1) = mean(astar_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - astar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    %ystardelta_bigposs_aterc_br(i + sample_half,2) = mean(astar_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - astar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    %ystardelta_bigposs_aterc_br(i + sample_half,3) = mean(astar_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - astar_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    ydelta_bigposs_aterc_re(i + sample_half,1) = mean(ya_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) - ya_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)));
    ydelta_bigposs_aterc_re(i + sample_half,2) = mean(ya_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ) - ya_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ));
    ydelta_bigposs_aterc_re(i + sample_half,3) = mean(ya_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) - ya_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)));
    
    earndelta_bigposs_aterc_br(i + sample_half,1) = mean(w_br*yi(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    earndelta_bigposs_aterc_br(i + sample_half,2) = mean(w_br*yi(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    earndelta_bigposs_aterc_br(i + sample_half,3) = mean(w_br*yi(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    earndelta_bigposs_aterc_re(i + sample_half,1) = mean(w_re*yi(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) - w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)));
    earndelta_bigposs_aterc_re(i + sample_half,2) = mean(w_re*yi(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ) - w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ));
    earndelta_bigposs_aterc_re(i + sample_half,3) = mean(w_re*yi(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) - w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)));
    
    incdelta_bigposs_aterc_br(i + sample_half,1) = mean(w_br*yi(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) + rt_br*a_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) - rt_br*a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)))/mean(w_br*yi(i+sample_half-2, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)) + rt_br*a_sims_br(i+sample_half-2, temp_ind == 1 & a_sims_br(i+sample_half-1,:) < terc_temp(1)));
    incdelta_bigposs_aterc_br(i + sample_half,2) = mean(w_br*yi(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) + rt_br*a_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2))- w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) - rt_br*a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)))/mean(w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)) + rt_br*a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(1) & a_sims_br(i+sample_half-1,:) < terc_temp(2)));
    incdelta_bigposs_aterc_br(i + sample_half,3) = mean(w_br*yi(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) + rt_br*a_sims_br(i+sample_half, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) - w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2))- rt_br*a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)))/mean(w_br*yi(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)) + rt_br*a_sims_br(i+sample_half-1, temp_ind == 1 & a_sims_br(i+sample_half-1,:) > terc_temp(2)));
    
    incdelta_bigposs_aterc_re(i + sample_half,1) = mean(w_re*yi(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) + rt_re*a_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1))- w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) - rt_re*a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)))/mean(w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)) + rt_re*a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) < terc_temp_re(1)));
    incdelta_bigposs_aterc_re(i + sample_half,2) = mean(w_re*yi(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ) + rt_re*a_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) )- w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) )-  rt_re*a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ))/mean(w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2) ) +  rt_re*a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(1) & a_sims_re(i+sample_half-1,:) < terc_temp_re(2)));
    incdelta_bigposs_aterc_re(i + sample_half,3) = mean(w_re*yi(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) + rt_re*a_sims_re(i+sample_half, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2))  - w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) - rt_re*a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)))/mean(w_re*yi(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)) + rt_re*a_sims_re(i+sample_half-1, temp_ind == 1 & a_sims_re(i+sample_half-1,:) > terc_temp_re(2)));
    
    

end

coh_dev_sims_br = ya - avg_coh_sims_br; 
coh_dev_sims_re = w_re*yi + (1+rt_re)*a_sims_re - avg_coh_sims_re; 

[breg_br,bint,~,~,stats] = regress( mpc_br2(end,:)', [ones(nsim,1), avg_coh_sims_br(end,:)',coh_dev_sims_br(end,:)']);
[breg_re,bint,~,~,stats] = regress( mpc_re(end,:)', [ones(nsim,1), avg_coh_sims_re(end,:)',coh_dev_sims_re(end,:)']);
[breg,bint,~,~,stats] = regress( mpc_br2(end,:)', [ones(nsim,1), ya(end,:)']);

mpc_var_share(1,1) = breg_br(2)^2*nanvar(avg_coh_sims_br(end,:)')/(breg_br(2)^2*nanvar(avg_coh_sims_br(end,:)') + breg_br(3)^2*nanvar( coh_dev_sims_br(end,:)'));
mpc_var_share(1,2) = breg_re(2)^2*nanvar(avg_coh_sims_re(end,:)')/(breg_re(2)^2*nanvar(avg_coh_sims_re(end,:)') + breg_re(3)^2*nanvar( coh_dev_sims_re(end,:)'));

coh_dev_sims_br = ya - ma_coh_sims_br; 
coh_dev_sims_re = w_re*yi + (1+rt_re)*a_sims_re - ma_coh_sims_re; 

[breg_br,bint,~,~,stats] = regress( mpc_br2(end,:)', [ones(nsim,1), ma_coh_sims_br(end,:)',coh_dev_sims_br(end,:)']);
[breg_re,bint,~,~,stats] = regress( mpc_re(end,:)', [ones(nsim,1), ma_coh_sims_re(end,:)',coh_dev_sims_re(end,:)']);
[breg,bint,~,~,stats] = regress( mpc_br2(end,:)', [ones(nsim,1), ya(end,:)']);

mpc_var_share(1,1) = breg_br(2)^2*nanvar(ma_coh_sims_br(end,:)')/(breg_br(2)^2*nanvar(ma_coh_sims_br(end,:)') + breg_br(3)^2*nanvar( coh_dev_sims_br(end,:)'));
mpc_var_share(1,2) = breg_re(2)^2*nanvar(ma_coh_sims_re(end,:)')/(breg_re(2)^2*nanvar(ma_coh_sims_re(end,:)') + breg_re(3)^2*nanvar( coh_dev_sims_re(end,:)'));



figure
subplot(3,2,1)
plot([nanmean(active_savings_medaquint_br)',nanmean(active_savings_cstar_medaquint_br)']*100, 'LineWidth',2)
ylabel( 'savings rate (%)')
xlabel( 'wealth quintile')
legend( 'BR model', 'RE model')
title( 'Annual Median Savings Rate')
subplot(3,2,2)
plot([nanmean(active_savings_medaquint_br2)',nanmean(active_savings_cstar_medaquint_br2)']*100, 'LineWidth',2)
ylabel( 'savings rate (%)')
xlabel( 'wealth quintile')
title( 'lifetime avg Median Savings Rate')
subplot(3,2,3)
plot([nanmean(active_savings_avgaquint_br)',nanmean(active_savings_cstar_avgaquint_br)']*100, 'LineWidth',2)
ylabel( 'savings rate (%)')
xlabel( 'wealth quintile')
legend( 'BR model', 'RE model')
title( 'Annual Mean Savings Rate')
subplot(3,2,4)
plot([nanmean(active_savings_avgaquint_br2)',nanmean(active_savings_cstar_avgaquint_br2)']*100, 'LineWidth',2)
ylabel( 'savings rate (%)')
xlabel( 'wealth quintile')
title( 'lifetime avg Mean Savings Rate')
subplot(3,2,5)
plot([nanmean(actsav_medaquint_br)',nanmean(actsav_cstar_medaquint_br)']*100, 'LineWidth',2)
ylabel( 'savings rate (%)')
xlabel( 'wealth quintile')
legend( 'BR model', 'RE model')
title( 'Annual Median Savings Rate (Counter-factual w)')
subplot(3,2,6)
plot([nanmean(actsav_avgaquint_br)',nanmean(actsav_cstar_avgaquint_br)']*100, 'LineWidth',2)
ylabel( 'savings rate (%)')
xlabel( 'wealth quintile')
title( 'Annual Mean Savings Rate (Counter-factual w)')

print -dpdf savings_rate.pdf


figure
plot(100*[nanmean(actsav_stdaquint_br)',nanmean(actsav_cstar_stdaquint_br)'], 'LineWidth',2)
ylabel( 'std(savings rate) (%)')
xlabel( 'wealth quintile')
title( 'Std Savings Rate (Counter-factual w)')


scatter( ya_dist_abs(unconstr_ind == 1), mpc_temp2( unconstr_ind == 1))
scatter( ya_dist_abs(end, unconstr_ind(end,:) == 1), mpc_temp2( end, unconstr_ind(end,:) == 1))

nanmedian( ya_dist_abs( unconstr_ind == 1 &  cross_alpha0  == 0))
nanmedian( ya_dist_abs( unconstr_ind == 1 &  cross_alpha0  == 1))

htm_sims_br_fullsample    = a_sims_br  <= w_br.*yi /6;



iter = 0; 

trap_freq_lifetime = NaN(total_agents,1);
trap_first_period  = NaN(total_agents,3); 
trap_Tlifelength  = NaN(total_agents,3); 
trap_prob_outoftrap  = NaN(total_agents,3); 
trap_prob_outoftrap_ever  = NaN(total_agents,3); 
rho_ya            = NaN(total_agents,1); 
rho_ya_trap       = NaN(total_agents,1); 
rho_ya_notrap     = NaN(total_agents,1);

rho_yadist            = NaN(total_agents,1); 
rho_yadist_trap       = NaN(total_agents,1); 
rho_yadist_notrap     = NaN(total_agents,1);

Ucstar        = NaN(total_agents,1); 
Uchat         = NaN(total_agents,1); 
Ucstar_notrap = NaN(total_agents,1); 
Uchat_notrap  = NaN(total_agents,1); 
Ucstar_trap = NaN(total_agents,1); 
Uchat_trap  = NaN(total_agents,1); 

cequiv_star        = Ucstar; 
cequiv_star_notrap = Ucstar; 
cequiv_star_trap = Ucstar; 

cequiv_br        = Ucstar; 
cequiv_br_notrap = Ucstar; 
cequiv_br_trap = Ucstar; 

wealth_quint_init = NaN(total_agents,1); 
wealth_quint_trap = NaN(total_agents,1); 

a_trap  = NaN(total_agents,1);
a_init  = NaN(total_agents,1);

life_length = NaN(total_agents,1); 

lifetime_earn_br  = NaN(total_agents,1); 
lifetime_income_br = NaN(total_agents,1); 
lifetime_earn_re  = NaN(total_agents,1); 
lifetime_income_re = NaN(total_agents,1); 
aend_br = NaN(total_agents,1); 
aend_re = NaN(total_agents,1); 

for i = 1:nsim
    
    ind_start = find(start_period(:,i) == 1);
    ind_end   = [ind_start(2:end)-1;sim_periods];
    
    for j = 1:length(ind_start)
        
        if ind_start(j) < sample_half
            continue
        end
        
        iter = iter + 1;
        
        
        
        life_length_temp   = age_sims_br(ind_end(j),i);
        life_length(iter)  = life_length_temp; 
        
        quint_temp = prctile( a_sims_br(ind_start(j),:), 100*[0.2, 0.4, 0.6, 0.8]);
        if ~isempty( find( a_sims_br(ind_start(j),i) >= quint_temp, 1, 'last') )
            wealth_quint_init(iter) = find( a_sims_br(ind_start(j),i) >= quint_temp, 1, 'last') + 1; 
        else
            wealth_quint_init(iter)  = 1;
        end
        
        a_init(iter) = a_sims_br(ind_start(j),i); 
        
        trap_freq_lifetime(iter) = mean(shat_cross(ind_start(j):ind_end(j),i) <  kappa/(2*W_cc));
        cross_temp = shat_cross(ind_start(j):ind_end(j),i) <  kappa/(2*W_cc);
        htm_ind_temp = htm_sims_br_fullsample(ind_start(j):ind_end(j),i); 
        ind_temp = find(shat_cross(ind_start(j):ind_end(j),i) <  kappa/(2*W_cc),1, 'first');
        
        if ~isempty(ind_temp)
            trap_first_period(iter,1) = ind_temp;
            trap_Tlifelength(iter,1)  = ind_temp/life_length_temp;
            if ind_temp <length(cross_temp)
                trap_prob_outoftrap(iter,1)  = mean( cross_temp(ind_temp+1:end) == 0);
                trap_prob_outoftrap_ever(iter,1)  = sum( cross_temp(ind_temp+1:end) == 0)>0;
                
            end
            
            if htm_ind_temp(ind_temp) == 1
                trap_first_period(iter,2) = ind_temp;
                trap_Tlifelength(iter,2)  = ind_temp/life_length_temp;
                if ind_temp <length(cross_temp)
                    trap_prob_outoftrap(iter,2)  = mean( cross_temp(ind_temp+1:end) == 0);
                    trap_prob_outoftrap_ever(iter,2)  = sum( cross_temp(ind_temp+1:end) == 0)>0;
                    
                end
            else
                trap_first_period(iter,3) = ind_temp;
                trap_Tlifelength(iter,3)  = ind_temp/life_length_temp;
                if ind_temp <length(cross_temp)
                    trap_prob_outoftrap(iter,3)  = mean( cross_temp(ind_temp+1:end) == 0);
                    trap_prob_outoftrap_ever(iter,3)  = sum( cross_temp(ind_temp+1:end) == 0)>0;
                    
                end
            end
            
        end
        
        if ~isempty(ind_temp)

            ctemp_star = NaN(life_length_temp - ind_temp+1,1); 
            atemp_star = ctemp_star; 
            astar0     = a_sims_br(ind_start(j):ind_end(j),i); 
            astar0     = astar0(ind_temp); 
            yi_temp    = yi(ind_start(j):ind_end(j),i); 
            yi_temp    = yi_temp(ind_temp:end); 
            
    
            ctemp_star(1)    = min(cstar_brsim(astar0*(1+rt_br) + w_br*yi_temp(1)), astar0*(1+rt_br) + w_br*yi_temp(1) - b);
            atemp_star(1)    = astar0; 

            for i2 = 2:length(ctemp_star)

                atemp_star(i2)    = w_br*yi_temp(i2-1) + (1+rt_br)*atemp_star(i2-1) - ctemp_star(i2-1);
                ctemp_star(i2)    = min(cstar_brsim(atemp_star(i2)*(1+rt_br) + w_br*yi_temp(i2)), atemp_star(i2)*(1+rt_br) + w_br*yi_temp(i2) - b);
            end
            
            a0_temp =  a_sims_br(ind_start(j):ind_end(j),:); 
            quint_temp = prctile( a0_temp(ind_temp,:), 100*[0.2, 0.4, 0.6, 0.8]);
            a_trap(iter) = a0_temp(ind_temp,i);
            
            if ~isempty( find( a0_temp(ind_temp,i) >= quint_temp, 1, 'last') )
                wealth_quint_trap(iter) = find( a0_temp(ind_temp,i) >= quint_temp, 1, 'last') + 1;
            else
                wealth_quint_trap(iter)  = 1;
            end
            
        end
        
        
        if life_length_temp > 10
            b_temp = regress( ya(ind_start(j)+1:ind_end(j),i), [ones(length(ya(ind_start(j):ind_end(j),i))-1,1),ya(ind_start(j):ind_end(j)-1,i)]);
            rho_ya(iter) = b_temp(2);
            
            b_temp = regress( abs( ya_cross(ind_start(j)+1:ind_end(j),i) - ya(ind_start(j)+1:ind_end(j),i)), [ones(length(ya(ind_start(j):ind_end(j),i))-1,1),abs( ya_cross(ind_start(j):ind_end(j)-1,i) - ya(ind_start(j):ind_end(j)-1,i))]);
            rho_yadist(iter) = b_temp(2);
            
            ya_indiv     =  ya(ind_start(j):ind_end(j),i);
            yadist_indiv =  abs(ya_cross(ind_start(j):ind_end(j),i) - ya(ind_start(j):ind_end(j),i));
            
            if length(ya_indiv) - ind_temp > 10
                b_temp = regress( ya_indiv(ind_temp+1:end), [ones(length(ya_indiv(ind_temp+1:end)),1),ya_indiv(ind_temp:end-1)]);
                rho_ya_trap(iter) = b_temp(2);
                
                b_temp = regress( yadist_indiv(ind_temp+1:end), [ones(length(yadist_indiv(ind_temp+1:end)),1),yadist_indiv(ind_temp:end-1)]);
                rho_yadist_trap(iter) = b_temp(2);
            end
            
            if  ind_temp > 10
                b_temp = regress( ya_indiv(2:ind_temp), [ones(length(ya_indiv(2:ind_temp)),1),ya_indiv(1:ind_temp-1)]);
                rho_ya_notrap(iter) = b_temp(2);
                
                b_temp = regress( yadist_indiv(2:ind_temp), [ones(length(yadist_indiv(2:ind_temp)),1),yadist_indiv(1:ind_temp-1)]);
                rho_yadist_notrap(iter) = b_temp(2);
            end
            
        end
        
        %%%%%%%%%%%% Welfare
        
        ctemp = cstar_sims_br( ind_start(j):ind_end(j), i);
        utemp = util(ctemp, util_type, gam);
        
        Ucstar(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        ctemp = cstar_sims_br( ind_start(j):ind_end(j), i);
        utemp = util(ctemp(1:ind_temp-1), util_type, gam);
        
        Ucstar_notrap(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        ctemp = chat_sims_br( ind_start(j):ind_end(j), i);
        utemp = util(ctemp, util_type, gam);
        
        Uchat(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        ctemp = chat_sims_br( ind_start(j):ind_end(j), i);
        utemp = util(ctemp(1:ind_temp-1), util_type, gam);
        
        Uchat_notrap(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        
        if ~isempty(ind_temp)

            ctemp = chat_sims_br( ind_start(j):ind_end(j), i);
            utemp = util(ctemp(ind_temp:end), util_type, gam);

            Uchat_trap(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));

            ctemp = ctemp_star;
            utemp = util(ctemp , util_type, gam);

            Ucstar_trap(iter) = sum(utemp.*(bsxfun(@power, btat, 0:length(utemp)-1)'));
        end
        
        bta_mult = (1 - btat.^(life_length_temp+1))/(1-btat);
        if ~isempty(ind_temp)
            bta_mult_notrap = (1 - btat.^(ind_temp))/(1-btat);
            bta_mult_trap   = (1 - btat.^(life_length_temp - ind_temp + 1))/(1-btat);
        else 
            bta_mult_notrap = NaN; 
            bta_mult_trap = NaN; 

        end
        %bta_mult = 1/(1-bta);
        
        if gam == 1
            cequiv_star(iter)  = exp(Ucstar(iter)/bta_mult);
            cequiv_star_notrap(iter) = exp(Ucstar_notrap(iter)/bta_mult_notrap);
            cequiv_star_trap(iter) = exp(Ucstar_trap(iter)/bta_mult_trap);

            cequiv_br(iter)  = exp(Uchat(iter)/bta_mult);
            cequiv_br_notrap(iter) = exp(Uchat_notrap(iter)/bta_mult_notrap);
            cequiv_br_trap(iter) = exp(Uchat_trap(iter)/bta_mult_trap);

        else
            cequiv_star(iter)  = (Ucstar(iter)./bta_mult*(1-gam) + 1).^(1/(1-gam));
            cequiv_star_notrap(iter)  = (Ucstar_notrap(iter)./bta_mult_notrap*(1-gam) + 1).^(1/(1-gam));
            cequiv_star_trap(iter)  = (Ucstar_trap(iter)./bta_mult_trap*(1-gam) + 1).^(1/(1-gam));

            cequiv_br(iter)  = (Uchat(iter)./bta_mult*(1-gam) + 1).^(1/(1-gam));
            cequiv_br_notrap(iter)  = (Uchat_notrap(iter)./bta_mult_notrap*(1-gam) + 1).^(1/(1-gam));
            cequiv_br_trap(iter)  = (Uchat_trap(iter)./bta_mult_trap*(1-gam) + 1).^(1/(1-gam));

        end
        
        %%%%%%%% Lifetime earnings and income
        
%         if iter == 2166 
%             error('blah')
%         end
        disc_temp = (1+rt_br).^((ind_start(j):ind_end(j)) - ind_start(j));
        %disc_temp =1; 
        
        %lifetime_earn_br(j,i) = mean(w_br*yi(ind_start:j,i).*(disc_temp(end:-1:1)'));
        %lifetime_income_br(j,i) = mean((rt_br*a_sims_br(ind_start:j,i) + w_br*yi(ind_start:j,i)).*(disc_temp(end:-1:1)'));
        lifetime_earn_br(iter) = sum(w_br*yi( ind_start(j):ind_end(j), i)./(disc_temp'));
        %lifetime_earn_sims_br(j,i) =   sum(w_br*(yi(ind_start:j,i)-1) );
        lifetime_income_br(iter) = sum((rt_br*a_sims_br(ind_start(j):ind_end(j),i) + w_br*yi(ind_start(j):ind_end(j),i))./(disc_temp'));
        %lifetime_income_br(iter) = a_sims_br(ind_start(j),i) + sum(( w_br*yi( ind_start(j):ind_end(j), i))./(disc_temp'));
        %lifetime_income_br(iter) = a_sims_br(ind_start(j), i).*disc_temp(end) + sum(( w_br*yi( ind_start(j):ind_end(j), i)).*(disc_temp(end:-1:1)'));
        aend_br(iter) = a_sims_br(ind_end(j),i); 
        
        disc_temp = (1+rt_re).^((ind_start(j):ind_end(j)) - ind_start(j));
        %disc_temp =1; 

        %lifetime_earn_re(j,i) = mean(w_re*yi(ind_start:j,i).*(disc_temp(end:-1:1)'));
        %lifetime_income_re(j,i) = mean((rt_re*a_sims_re(ind_start:j,i) + w_re*yi(ind_start:j,i)).*(disc_temp(end:-1:1)'));
        lifetime_earn_re(iter) = sum(w_re*yi( ind_start(j):ind_end(j), i)./(disc_temp'));
        %lifetime_earn_sims_br(j,i) =   sum(w_re*(yi(ind_start:j,i)-1) );
        lifetime_income_re(iter) = sum((rt_re*a_sims_re(ind_start(j):ind_end(j),i) + w_re*yi(ind_start(j):ind_end(j),i))./(disc_temp'));
        %lifetime_income_re(iter) = a_sims_re(ind_start(j),i) + sum(( w_re*yi( ind_start(j):ind_end(j), i))./(disc_temp'));
        %lifetime_income_re(iter) = a_sims_re(ind_start(j),i).*disc_temp(end) + sum( (w_re*yi( ind_start(j):ind_end(j), i)).*(disc_temp(end:-1:1)'));
        aend_re(iter) = a_sims_re(ind_end(j),i); 

        
        
    end
    
    
end


quint_temp = prctile( lifetime_earn_br, 100*linspace(0.1,0.9,9)); 
gini_wealth_lifearn_br = NaN( length(quint_temp) + 1,1); 

ind_temp = find( lifetime_earn_br < quint_temp(1)); 
gini_wealth_lifearn_br(1) = gini(ones(length(ind_temp),1), aend_br(ind_temp)); 
    
for i = 2:length(quint_temp) 
    ind_temp = find( lifetime_earn_br > quint_temp(i-1) & lifetime_earn_br <= quint_temp(i)); 
    gini_wealth_lifearn_br(i) = gini(ones(length(ind_temp),1), aend_br(ind_temp)); 

    
end

ind_temp = find( lifetime_earn_br > quint_temp(end)); 
gini_wealth_lifearn_br(end) = gini(ones(length(ind_temp),1), aend_br(ind_temp)); 

quint_temp = prctile( lifetime_income_br, 100*linspace(0.1,0.9,9)); 
gini_wealth_lifinc_br = NaN( length(quint_temp) + 1,1); 

ind_temp = find( lifetime_income_br < quint_temp(1)); 
gini_wealth_lifinc_br(1) = gini(ones(length(ind_temp),1), aend_br(ind_temp)); 
    
for i = 2:length(quint_temp) 
    ind_temp = find( lifetime_income_br > quint_temp(i-1) & lifetime_income_br <= quint_temp(i)); 
    gini_wealth_lifinc_br(i) = gini(ones(length(ind_temp),1), aend_br(ind_temp)); 

    
end

ind_temp = find( lifetime_income_br > quint_temp(end)); 
gini_wealth_lifinc_br(end) = gini(ones(length(ind_temp),1), aend_br(ind_temp)); 


quint_temp = prctile( lifetime_earn_re, 100*linspace(0.1,0.9,9)); 
gini_wealth_lifearn_re = NaN( length(quint_temp) + 1,1); 

ind_temp = find( lifetime_earn_re < quint_temp(1)); 
gini_wealth_lifearn_re(1) = gini(ones(length(ind_temp),1), aend_re(ind_temp)); 

for i = 2:length(quint_temp) 
    ind_temp = find( lifetime_earn_re > quint_temp(i-1) & lifetime_earn_re <= quint_temp(i)); 
    gini_wealth_lifearn_re(i) = gini(ones(length(ind_temp),1), aend_re(ind_temp)); 

    
end

ind_temp = find( lifetime_earn_re > quint_temp(end)); 
gini_wealth_lifearn_re(end) = gini(ones(length(ind_temp),1), aend_re(ind_temp)); 


quint_temp = prctile( lifetime_income_re, 100*linspace(0.1,0.9,9));  
gini_wealth_lifinc_re = NaN( length(quint_temp) + 1 ,1); 

ind_temp = find( lifetime_income_re < quint_temp(1)); 
gini_wealth_lifinc_re(1) = gini(ones(length(ind_temp),1), aend_re(ind_temp)); 
    
for i = 2:length(quint_temp) 
    ind_temp = find( lifetime_income_re > quint_temp(i-1) & lifetime_income_re <= quint_temp(i)); 
    gini_wealth_lifinc_re(i) = gini(ones(length(ind_temp),1), aend_re(ind_temp)); 

    
end

ind_temp = find( lifetime_income_re > quint_temp(end)); 
gini_wealth_lifinc_re(end) = gini(ones(length(ind_temp),1), aend_re(ind_temp)); 


[gini_wealth_lifearn_br, gini_wealth_lifearn_re, gini_wealth_lifinc_br, gini_wealth_lifinc_re]

%cequiv_star2  = exp(nanmean(Ucstar))*(1-btat); 
%cequiv_br2  = exp(nanmean(Uchat))*(1-btat); 

chat_temp = chat_sims_br(sample_half+1:end,:); 
cstar_temp = min(cstar_brsim(ya), ya - b);
cstar_temp = cstar_temp(sample_half+1:end,:); 

alpha_prime_temp = alpha_temp(2:end,:); 
cross_alpha0_lag = cross_alpha0(1:end-1,:); 
alpha_lag_temp   = alpha_temp(1:end-1,:); 
cross_alpha0_prime = real(cross_alpha0(2:end,:)); 
cross_alpha0_prime( age_temp(2:end,:) == 1) = NaN; 
age_temp_lag  = age_temp(1:end-1,:); 

ya_dist_shrink = ya_dist_abs(2:end,:)./ya_dist_abs(1:end-1,:); 
age_temp       = age_sims_br(sample_half+1:end,:); 

%%%%% Regressions
ya_dist_lag = ya_dist_abs(1:end-1,:); 
ya_dist_temp = ya_dist_abs(2:end,:); 
ya_dist_temp( age_temp(2:end,:) == 1) = NaN;

[b_yadist, bint,~,~,stats] = regress( ya_dist_temp(:), [ones(length(ya_dist_temp(:)),1), ya_dist_lag(:)]); 
[b_yadist_trap, bint,~,~,stats] = regress( ya_dist_temp(cross_alpha0_lag(:) ==1), [ones(length(ya_dist_temp(cross_alpha0_lag(:) ==1)),1), ya_dist_lag(cross_alpha0_lag(:) ==1)]); 
[b_yadist_notrap, bint,~,~,stats] = regress( ya_dist_temp(cross_alpha0_lag(:) ==0), [ones(length(ya_dist_temp(cross_alpha0_lag(:) ==0)),1), ya_dist_lag(cross_alpha0_lag(:) ==0)]); 

ya_lag = ya(sample_half +  1:end-1,:); 
ya_temp = ya(sample_half + 2:end,:); 
ya_temp( age_temp(2:end,:) == 1) = NaN;

[b_ya, bint,~,~,stats] = regress( ya_temp(:), [ones(length(ya_temp(:)),1), ya_lag(:)]); 
[b_ya_trap, bint,~,~,stats] = regress( ya_temp(cross_alpha0_lag(:) ==1), [ones(length(ya_temp(cross_alpha0_lag(:) ==1)),1), ya_lag(cross_alpha0_lag(:) ==1)]); 
[b_ya_notrap, bint,~,~,stats] = regress( ya_temp(cross_alpha0_lag(:) ==0), [ones(length(ya_temp(cross_alpha0_lag(:) ==0)),1), ya_lag(cross_alpha0_lag(:) ==0)]); 

ya_cross_prime     = ya_cross(sample_half+2:end,:); 
ya_cross_lag       = ya_cross(sample_half+1:end-1,:); 
ya_cross_prime( age_temp(2:end,:) == 1) = NaN; 

corr_alpha_ybardist_trap = corrcoef( alpha_temp(cross_alpha0(:)==1  & age_temp(:) > 1), ya_dist_abs(cross_alpha0(:) == 1 & age_temp(:) > 1),'rows','pairwise'); 
corr_alpha_ybardist_trap = corr_alpha_ybardist_trap(1,2); 
corr_alpha_ybardist_notrap = corrcoef( alpha_temp(cross_alpha0(:)== 0  & age_temp(:) > 1), ya_dist_abs(cross_alpha0(:) == 0 & age_temp(:) > 1),'rows','pairwise'); 
corr_alpha_ybardist_notrap = corr_alpha_ybardist_notrap(1,2); 


corr_alpha_ybardist_trap_posa = corrcoef( alpha_temp(cross_alpha0(:)==1 & alpha_temp(:) >0 & age_temp(:) > 1), ya_dist_abs(cross_alpha0(:) == 1 & alpha_temp(:) >0 & age_temp(:) > 1),'rows','pairwise'); 
corr_alpha_ybardist_trap_posa = corr_alpha_ybardist_trap_posa(1,2); 
corr_alpha_ybardist_notrap_posa = corrcoef( alpha_temp(cross_alpha0(:)== 0 & alpha_temp(:) >0 & age_temp(:) > 1), ya_dist_abs(cross_alpha0(:) == 0 & alpha_temp(:) >0 & age_temp(:) > 1) ,'rows','pairwise'); 
corr_alpha_ybardist_notrap_posa = corr_alpha_ybardist_notrap_posa(1,2); 

corr_alpha_maydist_trap = corrcoef( alpha_temp(cross_alpha0(:)==1  & age_temp(:) > 1), yma_dist_abs(cross_alpha0(:) == 1 & age_temp(:) > 1),'rows','pairwise'); 
corr_alpha_maydist_trap = corr_alpha_maydist_trap(1,2); 
corr_alpha_maydist_notrap = corrcoef( alpha_temp(cross_alpha0(:)== 0  & age_temp(:) > 1), yma_dist_abs(cross_alpha0(:) == 0 & age_temp(:) > 1),'rows','pairwise'); 
corr_alpha_maydist_notrap = corr_alpha_maydist_notrap(1,2); 

htm_ind_temp = htm_sims_br(2:end,:); 
htm_ind_lag  = htm_ind_temp(1:end-1,:); 

corr_alpha_ybardist_trap_htm = corrcoef( alpha_temp(cross_alpha0(:)==1  & age_temp(:) > 1 & htm_ind_temp(:) == 1 ), ya_dist_abs(cross_alpha0(:) == 1 & age_temp(:) > 1 & htm_ind_temp(:) == 1),'rows','pairwise'); 
corr_alpha_ybardist_trap_htm = corr_alpha_ybardist_trap_htm(1,2); 
corr_alpha_ybardist_notrap_htm = corrcoef( alpha_temp(cross_alpha0(:)== 0  & age_temp(:) > 1 & htm_ind_temp(:) == 1 ), ya_dist_abs(cross_alpha0(:) == 0 & age_temp(:) > 1 & htm_ind_temp(:) == 1),'rows','pairwise'); 
corr_alpha_ybardist_notrap_htm = corr_alpha_ybardist_notrap_htm(1,2); 

corr_alpha_ybardist_trap_nothtm = corrcoef( alpha_temp(cross_alpha0(:)==1  & age_temp(:) > 1 & htm_ind_temp(:) == 0 ), ya_dist_abs(cross_alpha0(:) == 1 & age_temp(:) > 1 & htm_ind_temp(:) == 0),'rows','pairwise'); 
corr_alpha_ybardist_trap_nothtm = corr_alpha_ybardist_trap_nothtm(1,2); 
corr_alpha_ybardist_notrap_nothtm = corrcoef( alpha_temp(cross_alpha0(:)== 0  & age_temp(:) > 1 & htm_ind_temp(:) == 0 ), ya_dist_abs(cross_alpha0(:) == 0 & age_temp(:) > 1 & htm_ind_temp(:) == 0),'rows','pairwise'); 
corr_alpha_ybardist_notrap_nothtm = corr_alpha_ybardist_notrap_nothtm(1,2); 


corr_alpha_ybardist_trap_htm_posa = corrcoef( alpha_temp(cross_alpha0(:)==1 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 1 ), ya_dist_abs(cross_alpha0(:) == 1 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 1 ),'rows','pairwise'); 
corr_alpha_ybardist_trap_htm_posa = corr_alpha_ybardist_trap_htm_posa(1,2); 
corr_alpha_ybardist_notrap_htm_posa = corrcoef( alpha_temp(cross_alpha0(:)== 0 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 1 ), ya_dist_abs(cross_alpha0(:) == 0 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 1 ) ,'rows','pairwise'); 
corr_alpha_ybardist_notrap_htm_posa = corr_alpha_ybardist_notrap_htm_posa(1,2); 

corr_alpha_ybardist_trap_nothtm_posa = corrcoef( alpha_temp(cross_alpha0(:)==1 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 0), ya_dist_abs(cross_alpha0(:) == 1 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 0 ),'rows','pairwise'); 
corr_alpha_ybardist_trap_nothtm_posa = corr_alpha_ybardist_trap_nothtm_posa(1,2); 
corr_alpha_ybardist_notrap_nothtm_posa = corrcoef( alpha_temp(cross_alpha0(:)== 0 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 0 ), ya_dist_abs(cross_alpha0(:) == 0 & alpha_temp(:) >0 & age_temp(:) > 1 & htm_ind_temp(:) == 0 ) ,'rows','pairwise'); 
corr_alpha_ybardist_notrap_nothtm_posa = corr_alpha_ybardist_notrap_nothtm_posa(1,2); 

corr_alpha_maydist_trap_htm = corrcoef( alpha_temp(cross_alpha0(:)==1  & age_temp(:) > 1 & htm_ind_temp(:) == 1), yma_dist_abs(cross_alpha0(:) == 1 & age_temp(:) > 1 & htm_ind_temp(:) == 1),'rows','pairwise'); 
corr_alpha_maydist_trap_htm = corr_alpha_maydist_trap_htm(1,2); 
corr_alpha_maydist_notrap_htm = corrcoef( alpha_temp(cross_alpha0(:)== 0  & age_temp(:) > 1 & htm_ind_temp(:) == 1), yma_dist_abs(cross_alpha0(:) == 0 & age_temp(:) > 1 & htm_ind_temp(:) == 1),'rows','pairwise'); 
corr_alpha_maydist_notrap_htm = corr_alpha_maydist_notrap_htm(1,2); 

corr_alpha_maydist_trap_nothtm   = corrcoef( alpha_temp(cross_alpha0(:)==1  & age_temp(:) > 1 & htm_ind_temp(:) == 0), yma_dist_abs(cross_alpha0(:) == 1 & age_temp(:) > 1 & htm_ind_temp(:) == 0),'rows','pairwise'); 
corr_alpha_maydist_trap_nothtm   = corr_alpha_maydist_trap_nothtm(1,2); 
corr_alpha_maydist_notrap_nothtm = corrcoef( alpha_temp(cross_alpha0(:)== 0  & age_temp(:) > 1 & htm_ind_temp(:) == 0), yma_dist_abs(cross_alpha0(:) == 0 & age_temp(:) > 1 & htm_ind_temp(:) == 0),'rows','pairwise'); 
corr_alpha_maydist_notrap_nothtm = corr_alpha_maydist_notrap_nothtm(1,2); 




%%
disp(' ');
disp(' --------------------------------')
disp([' Learning traps '])
disp(' --------------------------------')
disp( '                                                           all agents   HTM non-HTM  ');
disp(['   Frac shat(ybar) < targ                                 :' , sprintf('%1.4f   ', [ nanmean(cross_alpha0(:)), nanmean(cross_alpha0(htm_ind_temp == 1)), nanmean(cross_alpha0(htm_ind_temp == 0))])  ]);
disp(['   Frac shat(ybar) < targ  & alpha_t = 0                  :' , sprintf('%1.4f   ', [nanmean(cross_alpha0(:).*(alpha_temp(:) == 0)),nanmean(cross_alpha0(htm_ind_temp == 1).*(alpha_temp(htm_ind_temp == 1) == 0)),nanmean(cross_alpha0(htm_ind_temp == 0).*(alpha_temp(htm_ind_temp == 0) == 0))] )  ]);
disp(['   Frac alpha_t = 0    | shat(ybar < targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==1)  == 0), nanmean( alpha_temp(cross_alpha0(:)==1 & htm_ind_temp(:) == 1)  == 0), nanmean( alpha_temp(cross_alpha0(:)==1 & htm_ind_temp(:) == 0)  == 0)]    )  ]);
disp(['   Mean alpha_t        | shat(ybar < targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==1) ), nanmean( alpha_temp(cross_alpha0(:)==1 & htm_ind_temp(:) == 1) ),nanmean( alpha_temp(cross_alpha0(:)==1 & htm_ind_temp(:) == 0 ) )])  ]);
disp(['   Mean alpha_t        | shat(ybar < targ  & alpha_t > 0  :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==1 &  alpha_temp(:)> 0 )), nanmean( alpha_temp(cross_alpha0(:)==1 &  alpha_temp(:)> 0 & htm_ind_temp(:) == 1 )) , nanmean( alpha_temp(cross_alpha0(:)==1 &  alpha_temp(:)> 0 & htm_ind_temp(:) == 0 ))] )  ]);
disp(['   Mean alpha_{t+1}    | shat(ybar < targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1)), nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & htm_ind_lag(:) == 1)),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & htm_ind_lag(:) == 0))])   ]);
disp(['   Mean alpha_{t+1}    | shat(ybar < targ  & alpha_t = 0  :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & alpha_lag_temp(:) == 0)), nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 1)),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 0)) ]  )  ]);
disp(['   Prob alpha_{t+1} >0 | shat(ybar < targ  & alpha_t = 0  :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & alpha_lag_temp(:) == 0) >0),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 1) >0),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 0) >0)])  ]);
disp(['   Prob alpha_{t+1} >0 | shat(ybar < targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1) >0 ), nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & htm_ind_lag(:) == 1) >0 ),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==1 & htm_ind_lag(:) == 0) >0 ) ]  )  ]);
disp( '                     ');
disp(['   Frac shat(ybar) > targ  & alpha_t = 0                  :' , sprintf('%1.4f   ', [nanmean((cross_alpha0(:) == 0).*(alpha_temp(:) == 0)),nanmean((cross_alpha0(htm_ind_temp == 1) == 0).*(alpha_temp(htm_ind_temp == 1) == 0)),nanmean( (cross_alpha0(htm_ind_temp == 0)==0).*(alpha_temp(htm_ind_temp == 0) == 0))] )  ]);
disp(['   Frac alpha_t = 0    | shat(ybar > targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==0)  == 0), nanmean( alpha_temp(cross_alpha0(:)==0 & htm_ind_temp(:) == 1)  == 0), nanmean( alpha_temp(cross_alpha0(:)==0 & htm_ind_temp(:) == 0)  == 0)]    )  ]);
disp(['   Mean alpha_t        | shat(ybar > targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==0) ), nanmean( alpha_temp(cross_alpha0(:)==0 & htm_ind_temp(:) == 1) ),nanmean( alpha_temp(cross_alpha0(:)==0 & htm_ind_temp(:) == 0 ) )])  ]);
disp(['   Mean alpha_t        | shat(ybar > targ  & alpha_t > 0  :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==0 &  alpha_temp(:)> 0 )), nanmean( alpha_temp(cross_alpha0(:)==0 &  alpha_temp(:)> 0 & htm_ind_temp(:) == 1 )) , nanmean( alpha_temp(cross_alpha0(:)==0 &  alpha_temp(:)> 0 & htm_ind_temp(:) == 0 ))] )  ]);
disp(['   Mean alpha_{t+1}    | shat(ybar > targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0)), nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & htm_ind_lag(:) == 1)),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & htm_ind_lag(:) == 0))])   ]);
disp(['   Mean alpha_{t+1}    | shat(ybar > targ  & alpha_t = 0  :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & alpha_lag_temp(:) == 0)), nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 1)),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 0)) ]  )  ]);
disp(['   Prob alpha_{t+1} >0 | shat(ybar > targ  & alpha_t = 0  :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & alpha_lag_temp(:) == 0) >0),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 1) >0),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & alpha_lag_temp(:) == 0 & htm_ind_lag(:) == 0) >0)])  ]);
disp(['   Prob alpha_{t+1} >0 | shat(ybar > targ                 :' , sprintf('%1.4f   ', [nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0) >0 ), nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & htm_ind_lag(:) == 1) >0 ),nanmean( alpha_prime_temp(cross_alpha0_lag(:)==0 & htm_ind_lag(:) == 0) >0 ) ]  )  ]);
disp( '                                                            ');
disp(['   Mean alpha_t      | shat(ybar < targ                   :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==1)), nanmean( alpha_temp(cross_alpha0(:) ==1 & htm_ind_temp(:) == 1)),nanmean( alpha_temp(cross_alpha0(:) ==1 & htm_ind_temp(:) == 0))] )  ]);
disp(['   Mean alpha_t      | shat(ybar > targ                   :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==0)), nanmean( alpha_temp(cross_alpha0(:) ==0 & htm_ind_temp(:) == 1)),nanmean( alpha_temp(cross_alpha0(:) ==0 & htm_ind_temp(:) == 0))] )  ]);
disp(['   Prob (shat_{t+1}(ybar) > targ | shat_t(ybar) < targ)   :' , sprintf('%1.4f   ', [nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 1) == 0),nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 1 & htm_ind_lag(:) == 1) == 0),nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 1 & htm_ind_lag(:) == 0) == 0)   ])  ]);
disp(['   Prob (shat_{t+1}(ybar) < targ | shat_t(ybar) > targ)   :' , sprintf('%1.4f   ', [nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 0) == 1),nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 0 & htm_ind_lag(:) == 1) == 1),nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 0 & htm_ind_lag(:) == 0) == 1)   ])  ]);
disp(['   Mean   |yt - ybar|  | in-trap                          :' , sprintf('%1.4f   ', [ nanmean( ya_dist_abs(  cross_alpha0  == 1)),nanmean( ya_dist_abs(  cross_alpha0  == 1 & htm_ind_temp == 1)),nanmean( ya_dist_abs(  cross_alpha0  == 1 & htm_ind_temp == 0)) ]) ]); 
disp(['   Mean   |yt - ybar|  | out-of-trap                      :' , sprintf('%1.4f   ', [ nanmean( ya_dist_abs(  cross_alpha0  == 0)),nanmean( ya_dist_abs(  cross_alpha0  == 0 & htm_ind_temp == 1)),nanmean( ya_dist_abs(  cross_alpha0  == 0 & htm_ind_temp == 0)) ]) ]); 
disp(['   Corr( |yt - ybar|, alpha) | shat(ybar < targ           :' , sprintf('%1.4f   ', [corr_alpha_ybardist_trap, corr_alpha_ybardist_trap_htm, corr_alpha_ybardist_trap_nothtm]  )  ]);
disp(['   Corr( |yt - ybar|, alpha) | shat(ybar > targ           :' , sprintf('%1.4f   ', [corr_alpha_ybardist_notrap, corr_alpha_ybardist_notrap_htm, corr_alpha_ybardist_notrap_nothtm]  )  ]);
disp(['   Mean   |ybar_{t+1} - ybar_t|   | in-trap               :' , sprintf('%1.4f   ', [nanmean( abs(ya_cross_prime(cross_alpha0_lag == 1) - ya_cross_lag(cross_alpha0_lag == 1))), nanmean( abs(ya_cross_prime(cross_alpha0_lag == 1 & htm_ind_lag == 1) - ya_cross_lag(cross_alpha0_lag == 1 & htm_ind_lag == 1 ))), nanmean( abs(ya_cross_prime(cross_alpha0_lag == 1 & htm_ind_lag == 0) - ya_cross_lag(cross_alpha0_lag == 1 & htm_ind_lag == 0))) ]) ]); 
disp(['   Mean   |ybar_{t+1} - ybar_t|   | out-of-trap           :' , sprintf('%1.4f   ', [nanmean( abs(ya_cross_prime(cross_alpha0_lag == 0) - ya_cross_lag(cross_alpha0_lag == 0))), nanmean( abs(ya_cross_prime(cross_alpha0_lag == 0 & htm_ind_lag == 1) - ya_cross_lag(cross_alpha0_lag == 0 & htm_ind_lag == 1 ))), nanmean( abs(ya_cross_prime(cross_alpha0_lag == 0 & htm_ind_lag == 0) - ya_cross_lag(cross_alpha0_lag == 0 & htm_ind_lag == 0))) ]) ]); 
disp(['   MPC | trap                                             :' , sprintf('%1.4f   ', [nanmean(mpc_temp( cross_alpha0(:) == 1)), mean([mpc_trap_htm(:,1), mpc_trap_htm(:,2)])]   )]);
disp(['   MPC | out-of-trap                                      :' , sprintf('%1.4f   ', [nanmean(mpc_temp( cross_alpha0(:) == 0)), mean([mpc_notrap_htm(:,1), mpc_notrap_htm(:,2)])]   )]);
disp( '                     ');
disp(['   Trap freq. within lifetime                             :' , sprintf('%1.4f   ', nanmean( trap_freq_lifetime))  ]);
disp(['   Trap first period                                      :' , sprintf('%1.4f   ', nanmean( trap_first_period))  ]);
disp(['   Trap first period  / life_length                       :' , sprintf('%1.4f   ', nanmean( trap_Tlifelength))  ]);
disp(['   Prob (shat_{t+1}(ybar) > targ | shat_t(ybar) < targ)   :' , sprintf('%1.4f   ', [nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 1) == 0), nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 1 & htm_ind_lag(:) == 1) == 0),nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 1 & htm_ind_lag(:) == 0) == 0)  ])  ]);
disp(['   Prob (shat_{t+1}(ybar) < targ | shat_t(ybar) > targ)   :' , sprintf('%1.4f   ', [nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 0) == 1), nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 0 & htm_ind_lag(:) == 1) == 1), nanmean(cross_alpha0_prime( cross_alpha0_lag(:) == 0 & htm_ind_lag(:) == 0) == 1) ] )  ]);
disp(['   Freq out of trap after first period in trap            :' , sprintf('%1.4f   ', nanmean( trap_prob_outoftrap))  ]);
disp(['   Prob out of trap ever, after first period in trap      :' , sprintf('%1.4f   ', nanmean( trap_prob_outoftrap_ever))  ]);
disp( '                                                                 ');
disp(['   Fraction of Q1(a)  in trap                             :' , sprintf('%1.4f   ', mean(frac_intrap_aquint(:,1))) ]);
disp(['   Fraction of Q2(a)  in trap                             :' , sprintf('%1.4f   ', mean(frac_intrap_aquint(:,2))) ]);
disp(['   Fraction of Q3(a)  in trap                             :' , sprintf('%1.4f   ', mean(frac_intrap_aquint(:,3))) ]);
disp(['   Fraction of Q4(a)  in trap                             :' , sprintf('%1.4f   ', mean(frac_intrap_aquint(:,4))) ]);
disp(['   Fraction of Q5(a)  in trap                             :' , sprintf('%1.4f   ', mean(frac_intrap_aquint(:,5))) ]);
disp( '                           ');
disp(['   Corr( |yt - ybar|, alpha)                              :' , sprintf('%1.4f   ',  [corr(alpha_temp(age_temp(:) > 1), ya_dist_abs( age_temp(:) > 1)), corr(alpha_temp(age_temp(:) > 1 & htm_ind_temp(:) ==1), ya_dist_abs( age_temp(:) > 1 & htm_ind_temp(:) ==1 )), corr(alpha_temp(age_temp(:) > 1 & htm_ind_temp(:) ==0), ya_dist_abs( age_temp(:) > 1 & htm_ind_temp(:) == 0 )) ]  )  ]);
disp(['   Corr( |yt - ybar|, alpha) | shat(ybar < targ           :' , sprintf('%1.4f   ',  [corr_alpha_ybardist_trap, corr_alpha_ybardist_trap_htm, corr_alpha_ybardist_trap_nothtm] )  ]);
disp(['   Corr( |yt - ybar|, alpha) | shat(ybar < targ & alpha>0 :' , sprintf('%1.4f   ',  [corr_alpha_ybardist_trap_posa,corr_alpha_ybardist_trap_htm_posa,corr_alpha_ybardist_trap_nothtm_posa]    )  ]);
disp(['   Corr( |yt - ybar|, alpha) | shat(ybar > targ           :' , sprintf('%1.4f   ',  [corr_alpha_ybardist_notrap, corr_alpha_ybardist_notrap_htm, corr_alpha_ybardist_notrap_nothtm]   )  ]);
disp(['   Corr( |yt - ybar|, alpha) | shat(ybar > targ & alpha>0 :' , sprintf('%1.4f   ',  [corr_alpha_ybardist_notrap_posa, corr_alpha_ybardist_notrap_htm_posa, corr_alpha_ybardist_notrap_nothtm_posa] )  ]);
disp( '                           ');
disp(['   Corr( |yt - ymovavg|, alpha)                           :' , sprintf('%1.4f   ',  [corr(alpha_temp(age_temp(:) > 1), yma_dist_abs( age_temp(:) > 1)),corr(alpha_temp(age_temp(:) > 1 & htm_ind_temp(:) == 1), yma_dist_abs( age_temp(:) > 1 & htm_ind_temp(:) == 1)),corr(alpha_temp(age_temp(:) > 1 & htm_ind_temp(:) == 0), yma_dist_abs( age_temp(:) > 1 & htm_ind_temp(:) == 0)) ]  )  ]);
disp(['   Corr( |yt - ymovavg|, alpha) | shat(ybar < targ        :' , sprintf('%1.4f   ',  [corr_alpha_maydist_trap , corr_alpha_maydist_trap_htm , corr_alpha_maydist_trap_nothtm] )  ]);
disp(['   Corr( |yt - ymovavg|, alpha) | shat(ybar > targ        :' , sprintf('%1.4f   ',  [corr_alpha_maydist_notrap ,corr_alpha_maydist_notrap_htm , corr_alpha_maydist_notrap_nothtm]  )  ]);
disp( '                           ');
disp(['   Mean   |yt - ybar|  | in-trap                          :' , sprintf('%1.4f   ', [ nanmean( ya_dist_abs(  cross_alpha0  == 1)), nanmean( ya_dist_abs(  cross_alpha0  == 1 & htm_ind_temp == 1)), nanmean( ya_dist_abs(  cross_alpha0  == 1 & htm_ind_temp == 0 )) ]) ]); 
disp(['   Median |yt - ybar|  | in-trap                          :' , sprintf('%1.4f   ', [ nanmedian( ya_dist_abs(  cross_alpha0  == 1)), nanmedian( ya_dist_abs(  cross_alpha0  == 1 & htm_ind_temp == 1)) , nanmedian( ya_dist_abs(  cross_alpha0  == 1 & htm_ind_temp == 0))  ])  ]);
disp(['   Mean   |yt - ybar|  | out-of-trap                      :' , sprintf('%1.4f   ', [ nanmean( ya_dist_abs(  cross_alpha0  == 0)),nanmean( ya_dist_abs(  cross_alpha0  == 0 & htm_ind_temp == 1) ),nanmean( ya_dist_abs(  cross_alpha0  == 0 & htm_ind_temp == 0)) ]) ]); 
disp(['   Median |yt - ybar|  | out-of-trap                      :' , sprintf('%1.4f   ', [ nanmedian( ya_dist_abs(  cross_alpha0  == 0)),nanmedian( ya_dist_abs(  cross_alpha0  == 0 & htm_ind_temp == 1)),nanmedian( ya_dist_abs(  cross_alpha0  == 0 & htm_ind_temp == 0)) ])  ]);
disp( '                           ');
disp(['   Mean   |ybar_{t+1} - ybar_t|   | in-trap               :' , sprintf('%1.4f   ', [ nanmean( abs(ya_cross_prime(cross_alpha0_lag == 1) - ya_cross_lag(cross_alpha0_lag == 1))), nanmean( abs(ya_cross_prime(cross_alpha0_lag == 1 & htm_ind_lag == 1) - ya_cross_lag(cross_alpha0_lag == 1 & htm_ind_lag == 1))) , nanmean( abs(ya_cross_prime(cross_alpha0_lag == 1 & htm_ind_lag == 0) - ya_cross_lag(cross_alpha0_lag == 1 & htm_ind_lag == 0))) ]) ]); 
disp(['   Median |ybar_{t+1} - ybar_t|   | in-trap               :' , sprintf('%1.4f   ', [ nanmedian( abs(ya_cross_prime(cross_alpha0_lag == 1) - ya_cross_lag(cross_alpha0_lag == 1))), nanmedian( abs(ya_cross_prime(cross_alpha0_lag == 1 & htm_ind_lag == 1) - ya_cross_lag(cross_alpha0_lag == 1 & htm_ind_lag == 1))), nanmedian( abs(ya_cross_prime(cross_alpha0_lag == 1 & htm_ind_lag == 0) - ya_cross_lag(cross_alpha0_lag == 1 & htm_ind_lag == 0))) ])  ]);
disp(['   Mean   |ybar_{t+1} - ybar_t|   | out-of-trap           :' , sprintf('%1.4f   ', [ nanmean( abs(ya_cross_prime(cross_alpha0_lag == 0) - ya_cross_lag(cross_alpha0_lag == 0))), nanmean( abs(ya_cross_prime(cross_alpha0_lag == 0 & htm_ind_lag == 1) - ya_cross_lag(cross_alpha0_lag == 0 & htm_ind_lag == 1))) , nanmean( abs(ya_cross_prime(cross_alpha0_lag == 0 & htm_ind_lag == 0) - ya_cross_lag(cross_alpha0_lag == 0 & htm_ind_lag == 0))) ]) ]); 
disp(['   Median |ybar_{t+1} - ybar_t|   | out-of-trap           :' , sprintf('%1.4f   ', [ nanmedian( abs(ya_cross_prime(cross_alpha0_lag == 0) - ya_cross_lag(cross_alpha0_lag == 0))),nanmedian( abs(ya_cross_prime(cross_alpha0_lag == 0 & htm_ind_lag == 1) - ya_cross_lag(cross_alpha0_lag == 0 & htm_ind_lag == 1))) , nanmedian( abs(ya_cross_prime(cross_alpha0_lag == 0 & htm_ind_lag == 0) - ya_cross_lag(cross_alpha0_lag == 0 & htm_ind_lag == 0))) ])  ]);
disp( '                           ');
disp( '                           ');
disp(['   Mean   |y_t+1 - ybar|  | in-trap                       :' , sprintf('%1.4f   ', [ nanmean( ya_dist_temp(  cross_alpha0_lag  == 1))  ,nanmean( ya_dist_temp(  cross_alpha0_lag  == 1 & htm_ind_lag == 1)),nanmean( ya_dist_temp(  cross_alpha0_lag  == 1 & htm_ind_lag == 0)) ]) ]); 
disp(['   Median |y_t+1 - ybar|  | in-trap                       :' , sprintf('%1.4f   ', [ nanmedian( ya_dist_temp(  cross_alpha0_lag  == 1)),nanmedian( ya_dist_temp(  cross_alpha0_lag  == 1 & htm_ind_lag == 1)),nanmedian( ya_dist_temp(  cross_alpha0_lag  == 1 & htm_ind_lag == 0)) ])  ]);
disp(['   Mean   |y_t+1 - ybar|  | out-of-trap                   :' , sprintf('%1.4f   ', [ nanmean( ya_dist_temp(  cross_alpha0_lag  == 0))  ,nanmean( ya_dist_temp(  cross_alpha0_lag  == 0 & htm_ind_lag == 1)),nanmean( ya_dist_temp(  cross_alpha0_lag  == 0 & htm_ind_lag == 0)) ]) ]); 
disp(['   Median |y_t+1 - ybar|  | out-of-trap                   :' , sprintf('%1.4f   ', [ nanmedian( ya_dist_temp(  cross_alpha0_lag  == 0)),nanmedian( ya_dist_temp(  cross_alpha0_lag  == 0 & htm_ind_lag == 1)),nanmedian( ya_dist_temp(  cross_alpha0_lag  == 0 & htm_ind_lag == 0)) ])  ]);
disp( '                           ');
disp(['   Mean   yt - ybar  | in-trap                            :' , sprintf('%1.4f   ', [ nanmean( ya_dist(  cross_alpha0  == 1)), nanmean( ya_dist(  cross_alpha0  == 1 & htm_ind_temp == 1)), nanmean( ya_dist(  cross_alpha0  == 1 & htm_ind_temp == 0 )) ]) ]); 
disp(['   Median yt - ybar  | in-trap                            :' , sprintf('%1.4f   ', [ nanmedian( ya_dist(  cross_alpha0  == 1)), nanmedian( ya_dist(  cross_alpha0  == 1 & htm_ind_temp == 1)) , nanmedian( ya_dist(  cross_alpha0  == 1 & htm_ind_temp == 0))  ])  ]);
disp(['   Mean   yt - ybar  | out-of-trap                        :' , sprintf('%1.4f   ', [ nanmean( ya_dist(  cross_alpha0  == 0)),nanmean( ya_dist(  cross_alpha0  == 0 & htm_ind_temp == 1) ),nanmean( ya_dist(  cross_alpha0  == 0 & htm_ind_temp == 0)) ]) ]); 
disp(['   Median yt - ybar  | out-of-trap                        :' , sprintf('%1.4f   ', [ nanmedian( ya_dist(  cross_alpha0  == 0)),nanmedian( ya_dist(  cross_alpha0  == 0 & htm_ind_temp == 1)),nanmedian( ya_dist(  cross_alpha0  == 0 & htm_ind_temp == 0)) ])  ]);
disp( '                     ');
disp( '                                                           a < med(a)   a> med(a)  ');
disp(['   Frac shat(ybar) < targ                                 :' , sprintf('%1.4f   ', [ nanmean(cross_alpha0(unconstr_ind == 0)), nanmean(cross_alpha0(unconstr_ind == 1))])  ]);
disp(['   Frac shat(ybar) < targ  & alpha_t = 0                  :' , sprintf('%1.4f   ', [nanmean(cross_alpha0(unconstr_ind == 0).*(alpha_temp(unconstr_ind == 0) == 0)),nanmean(cross_alpha0(unconstr_ind == 1).*(alpha_temp(unconstr_ind == 1) == 0)) ])  ]);
disp(['   Frac alpha_t = 0  | shat(ybar < targ                   :' , sprintf('%1.4f   ', [ nanmean( alpha_temp(cross_alpha0==1 & (unconstr_ind == 0 ))  == 0), nanmean( alpha_temp(cross_alpha0 ==1 & (unconstr_ind == 1 ))  == 0)])  ]);
disp(['   Mean alpha_t      | shat(ybar < targ                   :' , sprintf('%1.4f   ', [ nanmean( alpha_temp(cross_alpha0(:)==1 & (unconstr_ind(:) == 0 )) ),  nanmean( alpha_temp(cross_alpha0(:)==1 & (unconstr_ind(:) == 1 )) )] ) ]);
disp(['   Mean alpha_t      | shat(ybar < targ  & alpha_t > 0    :' , sprintf('%1.4f   ', [nanmean( alpha_temp(cross_alpha0(:)==1 &  alpha_temp(:)> 0 & (unconstr_ind(:) == 0 ))), nanmean( alpha_temp(cross_alpha0(:)==1 &  alpha_temp(:)> 0 & (unconstr_ind(:) == 1 )))  ] ) ]);
disp( '                                                         ');
disp(['   Mean   |yt - ybar|  | in-trap                          :' , sprintf('%1.4f   ', [ nanmean( ya_dist_abs(    cross_alpha0  == 1 & unconstr_ind == 0)), nanmean( ya_dist_abs(    cross_alpha0  == 1 & unconstr_ind ==1)) ]) ]); 
disp(['   Median |yt - ybar|  | in-trap                          :' , sprintf('%1.4f   ', [ nanmedian( ya_dist_abs(  cross_alpha0  == 1 & unconstr_ind == 0)), nanmedian( ya_dist_abs(  cross_alpha0  == 1 & unconstr_ind == 1))])  ]);
disp(['   Mean   |yt - ybar|  | out-of-trap                      :' , sprintf('%1.4f   ', [ nanmean( ya_dist_abs(    cross_alpha0  == 0 & unconstr_ind == 0)), nanmean( ya_dist_abs(    cross_alpha0  == 0 & unconstr_ind == 1))]) ]); 
disp(['   Median |yt - ybar|  | out-of-trap                      :' , sprintf('%1.4f   ', [ nanmedian( ya_dist_abs(  cross_alpha0  == 0 & unconstr_ind == 0)), nanmedian( ya_dist_abs(  cross_alpha0  == 0 & unconstr_ind == 1))])  ]);
disp( '                                                         ');
disp( '                                                          : All     In-Trap  Out-of-Trap');
disp(['   Mean   rho_ya,  indiv                                  :' , sprintf('%1.4f   ',  nanmean( [rho_ya, rho_ya_trap, rho_ya_notrap]) ) ]); 
disp(['   Median rho_ya,  indiv                                  :' , sprintf('%1.4f   ',  nanmedian( [rho_ya, rho_ya_trap, rho_ya_notrap]))  ]);
disp(['   Mean   rho_yadist,  indiv                              :' , sprintf('%1.4f   ',  nanmean( [rho_yadist, rho_yadist_trap, rho_yadist_notrap]) ) ]); 
disp(['   Median rho_yadist,  indiv                              :' , sprintf('%1.4f   ',  nanmedian( [rho_yadist, rho_yadist_trap, rho_yadist_notrap]))  ]);
disp(['   rho_ya  pooled                                         :' , sprintf('%1.4f   ', [ b_ya(2), b_ya_trap(2), b_ya_notrap(2) ]) ]); 
disp(['   rho_yaybardist pooled                                  :' , sprintf('%1.4f   ', [ b_yadist(2), b_yadist_trap(2), b_yadist_notrap(2) ])  ]);
disp( '                           ');
disp( ' MPC                                                       All  In-trap   Not-trap  ');
disp(['   MPC | HtM                                              :' , sprintf('%1.4f   ', nanmean([mpc_htm(:,1), mpc_trap_htm(:,1), mpc_notrap_htm(:,1)])   )]);
disp(['   MPC | not HtM                                          :' , sprintf('%1.4f   ', nanmean([mpc_htm(:,2),mpc_trap_htm(:,2), mpc_notrap_htm(:,2)])   )]);
disp(['   MPC | Q1(a)                                            :' , sprintf('%1.4f   ', nanmean([mpc_aquint(:,1),mpc_trap_aquint(:,1), mpc_notrap_aquint(:,1)]) )]);
disp(['   MPC | Q2(a)                                            :' , sprintf('%1.4f   ', nanmean([mpc_aquint(:,2),mpc_trap_aquint(:,2), mpc_notrap_aquint(:,2)]) )]);
disp(['   MPC | Q3(a)                                            :' , sprintf('%1.4f   ', nanmean([mpc_aquint(:,3),mpc_trap_aquint(:,3), mpc_notrap_aquint(:,3)]) )]);
disp(['   MPC | Q4(a)                                            :' , sprintf('%1.4f   ', nanmean([mpc_aquint(:,4),mpc_trap_aquint(:,4), mpc_notrap_aquint(:,4)]) )]);
disp(['   MPC | Q5(a)                                            :' , sprintf('%1.4f   ', nanmean([mpc_aquint(:,5),mpc_trap_aquint(:,5), mpc_notrap_aquint(:,5)]) )]);
disp( '                           ');
disp( ' APC                                                        All  In-trap   Not-trap  ');
disp(['   APC | HtM                                              :' , sprintf('%1.4f   ', [mean(apc_temp( htm_ind_temp == 1)),mean([apc_trap_htm(:,1), apc_notrap_htm(:,1), apc_cstar_htm(:,1)])]   )]);
disp(['   APC | not HtM                                          :' , sprintf('%1.4f   ', [mean(apc_temp( htm_ind_temp == 0)),mean([apc_trap_htm(:,2), apc_notrap_htm(:,2), apc_cstar_htm(:,2)])]   )]);
disp(['   APC | Q1(a)                                            :' , sprintf('%1.4f   ', mean([apc_aquint(:,1), apc_trap_aquint(:,1), apc_notrap_aquint(:,1), apc_cstar_aquint(:,1)]) )]);
disp(['   APC | Q2(a)                                            :' , sprintf('%1.4f   ', mean([apc_aquint(:,2), apc_trap_aquint(:,2), apc_notrap_aquint(:,2), apc_cstar_aquint(:,2)]) )]);
disp(['   APC | Q3(a)                                            :' , sprintf('%1.4f   ', mean([apc_aquint(:,3), apc_trap_aquint(:,3), apc_notrap_aquint(:,3), apc_cstar_aquint(:,3)]) )]);
disp(['   APC | Q4(a)                                            :' , sprintf('%1.4f   ', mean([apc_aquint(:,4), apc_trap_aquint(:,4), apc_notrap_aquint(:,4), apc_cstar_aquint(:,4)]) )]);
disp(['   APC | Q5(a)                                            :' , sprintf('%1.4f   ', mean([apc_aquint(:,5), apc_trap_aquint(:,5), apc_notrap_aquint(:,5), apc_cstar_aquint(:,5)]) )]);
disp( '                                                           ');
disp(['   avgAPC | HtM                                           :' , sprintf('%1.4f   ', [mean(avg_apc_temp( htm_ind_temp == 1)),mean([avg_apc_trap_htm(:,1), avg_apc_notrap_htm(:,1), avg_apc_cstar_htm(:,1)]) ]  )]);
disp(['   avgAPC | not HtM                                       :' , sprintf('%1.4f   ', [mean(avg_apc_temp( htm_ind_temp == 0)),mean([avg_apc_trap_htm(:,2), avg_apc_notrap_htm(:,2), avg_apc_cstar_htm(:,2)]) ]  )]);
disp(['   avgAPC | Q1(a)                                         :' , sprintf('%1.4f   ', mean([avg_apc_aquint(:,1), avg_apc_trap_aquint(:,1), avg_apc_notrap_aquint(:,1), avg_apc_cstar_aquint(:,1)]) )]);
disp(['   avgAPC | Q2(a)                                         :' , sprintf('%1.4f   ', mean([avg_apc_aquint(:,2),avg_apc_trap_aquint(:,2), avg_apc_notrap_aquint(:,2), avg_apc_cstar_aquint(:,2)]) )]);
disp(['   avgAPC | Q3(a)                                         :' , sprintf('%1.4f   ', mean([avg_apc_aquint(:,3),avg_apc_trap_aquint(:,3), avg_apc_notrap_aquint(:,3), avg_apc_cstar_aquint(:,3)]) )]);
disp(['   avgAPC | Q4(a)                                         :' , sprintf('%1.4f   ', mean([avg_apc_aquint(:,4),avg_apc_trap_aquint(:,4), avg_apc_notrap_aquint(:,4), avg_apc_cstar_aquint(:,4)]) )]);
disp(['   avgAPC | Q5(a)                                         :' , sprintf('%1.4f   ', mean([avg_apc_aquint(:,5),avg_apc_trap_aquint(:,5), avg_apc_notrap_aquint(:,5), avg_apc_cstar_aquint(:,5)]) )]);
disp( '                                                           ');
disp(['   median yt - ybar  | Q1(a)                              :' , sprintf('%1.4f   ', mean([ya_dist_aquint(:,1), ya_dist_trap_aquint(:,1), ya_dist_notrap_aquint(:,1)]) )]);
disp(['   median yt - ybar  | Q2(a)                              :' , sprintf('%1.4f   ', mean([ya_dist_aquint(:,2),ya_dist_trap_aquint(:,2), ya_dist_notrap_aquint(:,2)]) )]);
disp(['   median yt - ybar  | Q3(a)                              :' , sprintf('%1.4f   ', mean([ya_dist_aquint(:,3),ya_dist_trap_aquint(:,3), ya_dist_notrap_aquint(:,3)]) )]);
disp(['   median yt - ybar  | Q4(a)                              :' , sprintf('%1.4f   ', mean([ya_dist_aquint(:,4),ya_dist_trap_aquint(:,4), ya_dist_notrap_aquint(:,4)]) )]);
disp(['   median yt - ybar  | Q5(a)                              :' , sprintf('%1.4f   ', mean([ya_dist_aquint(:,5),ya_dist_trap_aquint(:,5), ya_dist_notrap_aquint(:,5)]) )]);
disp( '                                                           ');
disp(['   median chat - crw  | Q1(a)                             :' , sprintf('%1.4f   ', mean([chat_crw_diff(:,1), chat_crw_diff_trap(:,1), chat_crw_diff_notrap(:,1)]) )]);
disp(['   median chat - crw  | Q2(a)                             :' , sprintf('%1.4f   ', mean([chat_crw_diff(:,2), chat_crw_diff_trap(:,2), chat_crw_diff_notrap(:,2)]) )]);
disp(['   median chat - crw  | Q3(a)                             :' , sprintf('%1.4f   ', mean([chat_crw_diff(:,3), chat_crw_diff_trap(:,3), chat_crw_diff_notrap(:,3)]) )]);
disp(['   median chat - crw  | Q4(a)                             :' , sprintf('%1.4f   ', mean([chat_crw_diff(:,4), chat_crw_diff_trap(:,4), chat_crw_diff_notrap(:,4)]) )]);
disp(['   median chat - crw  | Q5(a)                             :' , sprintf('%1.4f   ', mean([chat_crw_diff(:,5), chat_crw_diff_trap(:,5), chat_crw_diff_notrap(:,5)]) )]);
disp( '                           ');
disp( ' Predictable Drops in income                                BR  RE   BR-cstar  ');
disp([' delta inc | terc(1)                                      :' , sprintf('%1.4f   ', [nanmean(incdelta_bigposs_aterc_br(:,1)),nanmean(incdelta_bigposs_aterc_re(:,1))]) ]  );
disp([' delta inc | terc(2)                                      :' , sprintf('%1.4f   ', [nanmean(incdelta_bigposs_aterc_br(:,2)),nanmean(incdelta_bigposs_aterc_re(:,2))]) ]  );
disp([' delta inc | terc(3)                                      :' , sprintf('%1.4f   ', [nanmean(incdelta_bigposs_aterc_br(:,3)),nanmean(incdelta_bigposs_aterc_re(:,3))]) ]  );
disp(['   delta C | terc(1)                                      :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_aterc_br(:,1)),nanmean(cdelta_bigposs_aterc_re(:,1)),nanmean(cstardelta_bigposs_aterc_br(:,1))]) ]  );
disp(['   delta C | terc(2)                                      :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_aterc_br(:,2)),nanmean(cdelta_bigposs_aterc_re(:,2)),nanmean(cstardelta_bigposs_aterc_br(:,2))]) ]  );
disp(['   delta C | terc(3)                                      :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_aterc_br(:,3)),nanmean(cdelta_bigposs_aterc_re(:,3)),nanmean(cstardelta_bigposs_aterc_br(:,3))]) ]  );
disp(['   median delta C | terc(1)                               :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_medaterc_br(:,1)),NaN,NaN]) ]  );
disp(['   median delta C | terc(2)                               :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_medaterc_br(:,2)),NaN,NaN]) ]  );
disp(['   median delta C | terc(3)                               :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_medaterc_br(:,3)),NaN,NaN]) ]  );
disp(['   delta C | terc(1)   (%)                                :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_aterc_perc_br(:,1)),nanmean(cdelta_bigposs_aterc_perc_re(:,1)),nanmean(cstardelta_bigposs_aterc_perc_br(:,1))]) ]  );
disp(['   delta C | terc(2)   (%)                                :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_aterc_perc_br(:,2)),nanmean(cdelta_bigposs_aterc_perc_re(:,2)),nanmean(cstardelta_bigposs_aterc_perc_br(:,2))]) ]  );
disp(['   delta C | terc(3)   (%)                                :' , sprintf('%1.4f   ', [nanmean(cdelta_bigposs_aterc_perc_br(:,3)),nanmean(cdelta_bigposs_aterc_perc_re(:,3)),nanmean(cstardelta_bigposs_aterc_perc_br(:,3))]) ]  );
disp(['   delta A | terc(1)                                      :' , sprintf('%1.4f   ', [nanmean(adelta_bigposs_aterc_br(:,1)),nanmean(adelta_bigposs_aterc_re(:,1)),nanmean(astardelta_bigposs_aterc_br(:,1))]) ]  );
disp(['   delta A | terc(2)                                      :' , sprintf('%1.4f   ', [nanmean(adelta_bigposs_aterc_br(:,2)),nanmean(adelta_bigposs_aterc_re(:,2)),nanmean(astardelta_bigposs_aterc_br(:,2))]) ]  );
disp(['   delta A | terc(3)                                      :' , sprintf('%1.4f   ', [nanmean(adelta_bigposs_aterc_br(:,3)),nanmean(adelta_bigposs_aterc_re(:,3)),nanmean(astardelta_bigposs_aterc_br(:,3))]) ]  );
disp(['   delta Y | terc(1)                                      :' , sprintf('%1.4f   ', [nanmean(ydelta_bigposs_aterc_br(:,1)),nanmean(ydelta_bigposs_aterc_re(:,1)),nanmean(ystardelta_bigposs_aterc_br(:,1))]) ]  );
disp(['   delta Y | terc(2)                                      :' , sprintf('%1.4f   ', [nanmean(ydelta_bigposs_aterc_br(:,2)),nanmean(ydelta_bigposs_aterc_re(:,2)),nanmean(ystardelta_bigposs_aterc_br(:,2))]) ]  );
disp(['   delta Y | terc(3)                                      :' , sprintf('%1.4f   ', [nanmean(ydelta_bigposs_aterc_br(:,3)),nanmean(ydelta_bigposs_aterc_re(:,3)),nanmean(ystardelta_bigposs_aterc_br(:,3))]) ]  );
disp(['chat - crw | terc(1)                                      :' , sprintf('%1.4f   ', [nanmean(chatrwdiff_bigposs_aterc_br(:,1)),nanmean(cstarrwdiff_bigposs_aterc_re(:,1)),nanmean(cstarrwdiff_bigposs_aterc_br(:,1))]) ]  );
disp(['chat - crw | terc(2)                                      :' , sprintf('%1.4f   ', [nanmean(chatrwdiff_bigposs_aterc_br(:,2)),nanmean(cstarrwdiff_bigposs_aterc_re(:,2)),nanmean(cstarrwdiff_bigposs_aterc_br(:,2))]) ]  );
disp(['chat - crw | terc(3)                                      :' , sprintf('%1.4f   ', [nanmean(chatrwdiff_bigposs_aterc_br(:,3)),nanmean(cstarrwdiff_bigposs_aterc_re(:,3)),nanmean(cstarrwdiff_bigposs_aterc_br(:,3))]) ]  );

disp( '                                                            ');
disp( 'WELFARE LOSSES                         ');
disp( '                                                            All  In-trap   Not-trap  ');
disp(['  Mean |chat - cstar|                                     :' , sprintf('%1.4f   ',[ mean(abs( chat_temp(:)  - cstar_temp(:))) , mean(abs( chat_temp(cross_alpha0(:) == 1)  - cstar_temp(cross_alpha0(:) == 1))), mean(abs( chat_temp(cross_alpha0(:) == 0)  - cstar_temp(cross_alpha0(:) == 0)))] )]);
disp(['  Mean |chat - cstar|/cstar  (%)                          :' , sprintf('%1.4f   ',100*[ mean(abs( log(chat_temp(:))  - log(cstar_temp(:)))) , mean(abs( log(chat_temp(cross_alpha0(:) == 1))  - log(cstar_temp(cross_alpha0(:) == 1)))), mean(abs( log(chat_temp(cross_alpha0(:) == 0))  - log(cstar_temp(cross_alpha0(:) == 0))))] )]);
disp(['  Mean |chat - cstar|/cstar  (%)    | Q1(a)               :' , sprintf('%1.4f   ', 100*mean( abs([ chat_cstar_absdiff(:,1), chat_cstar_absdiff_trap(:,1), chat_cstar_absdiff_notrap(:,1) ]) ))]);
disp(['  Mean |chat - cstar|/cstar  (%)    | Q2(a)               :' , sprintf('%1.4f   ', 100*mean(abs([ chat_cstar_absdiff(:,2), chat_cstar_absdiff_trap(:,2), chat_cstar_absdiff_notrap(:,2) ]) ))]);
disp(['  Mean |chat - cstar|/cstar  (%)    | Q3(a)               :' , sprintf('%1.4f   ', 100*mean(abs([ chat_cstar_absdiff(:,3), chat_cstar_absdiff_trap(:,3), chat_cstar_absdiff_notrap(:,3) ]) ))]);
disp(['  Mean |chat - cstar|/cstar  (%)    | Q4(a)               :' , sprintf('%1.4f   ', 100*mean(abs([ chat_cstar_absdiff(:,4), chat_cstar_absdiff_trap(:,4), chat_cstar_absdiff_notrap(:,4) ]) ))]);
disp(['  Mean |chat - cstar|/cstar  (%)    | Q5(a)               :' , sprintf('%1.4f   ', 100*mean(abs([ chat_cstar_absdiff(:,5), chat_cstar_absdiff_trap(:,5), chat_cstar_absdiff_notrap(:,5) ]) ))]);
disp( '                                                            All  In-trap   Not-trap  ');
disp(['  Mean chat - cstar                                       :' , sprintf('%1.4f   ',[ mean(chat_temp(:)  - cstar_temp(:)) , mean( chat_temp(cross_alpha0(:) == 1)  - cstar_temp(cross_alpha0(:) == 1)), mean(chat_temp(cross_alpha0(:) == 0)  - cstar_temp(cross_alpha0(:) == 0))] )]);
disp(['  Mean (chat - cstar)/cstar  (%)                          :' , sprintf('%1.4f   ',100*[mean( log(chat_temp(:))  - log(cstar_temp(:))) , mean( log(chat_temp(cross_alpha0(:) == 1))  - log(cstar_temp(cross_alpha0(:) == 1))), mean( log(chat_temp(cross_alpha0(:) == 0))  - log(cstar_temp(cross_alpha0(:) == 0)))] )]);
disp(['  Mean (chat - cstar)/cstar  (%)    | Q1(a)               :' , sprintf('%1.4f   ', 100*mean([ chat_cstar_diff(:,1), chat_cstar_diff_trap(:,1), chat_cstar_diff_notrap(:,1) ]) )]);
disp(['  Mean (chat - cstar)/cstar  (%)    | Q2(a)               :' , sprintf('%1.4f   ', 100*mean([ chat_cstar_diff(:,2), chat_cstar_diff_trap(:,2), chat_cstar_diff_notrap(:,2) ]) )]);
disp(['  Mean (chat - cstar)/cstar  (%)    | Q3(a)               :' , sprintf('%1.4f   ', 100*mean([ chat_cstar_diff(:,3), chat_cstar_diff_trap(:,3), chat_cstar_diff_notrap(:,3) ]) )]);
disp(['  Mean (chat - cstar)/cstar  (%)    | Q4(a)               :' , sprintf('%1.4f   ', 100*mean([ chat_cstar_diff(:,4), chat_cstar_diff_trap(:,4), chat_cstar_diff_notrap(:,4) ]) )]);
disp(['  Mean (chat - cstar)/cstar  (%)    | Q5(a)               :' , sprintf('%1.4f   ', 100*mean([ chat_cstar_diff(:,5), chat_cstar_diff_trap(:,5), chat_cstar_diff_notrap(:,5) ]) )]);
disp( '                                                            ');
disp(['  Permanent Consumption Loss (%)                          :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star./cequiv_br)),nanmean(log( cequiv_star_trap./cequiv_br_trap)), nanmean(log( cequiv_star_notrap./cequiv_br_notrap))])  ]);
disp(['  Median Permanent Consumption Loss (%)                   :' , sprintf('%1.4f   ', 100*[nanmedian(log( cequiv_star./cequiv_br)),nanmedian(log( cequiv_star_trap./cequiv_br_trap)), nanmedian(log( cequiv_star_notrap./cequiv_br_notrap))])  ]);
disp( '                                                            ');
disp(['  Permanent Consumption Loss (%) | Q1(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 1)./cequiv_br(wealth_quint_init(:) == 1))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 1)./cequiv_br_trap(wealth_quint_init(:) == 1))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 1)./cequiv_br_notrap(wealth_quint_init(:) == 1)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q2(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 2)./cequiv_br(wealth_quint_init(:) == 2))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 2)./cequiv_br_trap(wealth_quint_init(:) == 2))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 2)./cequiv_br_notrap(wealth_quint_init(:) == 2)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q3(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 3)./cequiv_br(wealth_quint_init(:) == 3))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 3)./cequiv_br_trap(wealth_quint_init(:) == 3))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 3)./cequiv_br_notrap(wealth_quint_init(:) == 3)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q4(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 4)./cequiv_br(wealth_quint_init(:) == 4))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 4)./cequiv_br_trap(wealth_quint_init(:) == 4))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 4)./cequiv_br_notrap(wealth_quint_init(:) == 4)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q5(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 5)./cequiv_br(wealth_quint_init(:) == 5))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 5)./cequiv_br_trap(wealth_quint_init(:) == 5))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 5)./cequiv_br_notrap(wealth_quint_init(:) == 5)))])  ]);
disp( '                                                            ');
disp(['  Permanent Consumption Loss (%) | Q1(a) & life  > 10     :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 1 & life_length(:) > 10)./cequiv_br(wealth_quint_init(:) == 1 & life_length(:) > 10))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 1 & life_length(:) > 10)./cequiv_br_trap(wealth_quint_init(:) == 1 & life_length(:) > 10))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 1 & life_length(:) > 10)./cequiv_br_notrap(wealth_quint_init(:) == 1 & life_length(:) > 10)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q2(a) & life  > 10     :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 2 & life_length(:) > 10)./cequiv_br(wealth_quint_init(:) == 2 & life_length(:) > 10))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 2 & life_length(:) > 10)./cequiv_br_trap(wealth_quint_init(:) == 2 & life_length(:) > 10))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 2 & life_length(:) > 10)./cequiv_br_notrap(wealth_quint_init(:) == 2 & life_length(:) > 10)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q3(a) & life  > 10     :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 3 & life_length(:) > 10)./cequiv_br(wealth_quint_init(:) == 3 & life_length(:) > 10))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 3 & life_length(:) > 10)./cequiv_br_trap(wealth_quint_init(:) == 3 & life_length(:) > 10))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 3 & life_length(:) > 10)./cequiv_br_notrap(wealth_quint_init(:) == 3 & life_length(:) > 10)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q4(a) & life  > 10     :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 4 & life_length(:) > 10)./cequiv_br(wealth_quint_init(:) == 4 & life_length(:) > 10))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 4 & life_length(:) > 10)./cequiv_br_trap(wealth_quint_init(:) == 4 & life_length(:) > 10))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 4 & life_length(:) > 10)./cequiv_br_notrap(wealth_quint_init(:) == 4 & life_length(:) > 10)))])  ]);
disp(['  Permanent Consumption Loss (%) | Q5(a) & life  > 10     :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 5 & life_length(:) > 10)./cequiv_br(wealth_quint_init(:) == 5 & life_length(:) > 10))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 5 & life_length(:) > 10)./cequiv_br_trap(wealth_quint_init(:) == 5 & life_length(:) > 10))), nanmean(log( cequiv_star_notrap(wealth_quint_init(:) == 5 & life_length(:) > 10)./cequiv_br_notrap(wealth_quint_init(:) == 5 & life_length(:) > 10)))])  ]);
disp( '                                                            ');
disp( '                                                            All  In-trap   In-trap (A quintile at trap period)  ');
disp(['  Permanent Consumption Loss (%) | Q1(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 1)./cequiv_br(wealth_quint_init(:) == 1))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 1)./cequiv_br_trap(wealth_quint_init(:) == 1))), nanmean(log( cequiv_star_trap(wealth_quint_trap(:) == 1)./cequiv_br_trap(wealth_quint_trap(:) == 1))) ])  ]);
disp(['  Permanent Consumption Loss (%) | Q2(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 2)./cequiv_br(wealth_quint_init(:) == 2))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 2)./cequiv_br_trap(wealth_quint_init(:) == 2))), nanmean(log( cequiv_star_trap(wealth_quint_trap(:) == 2)./cequiv_br_trap(wealth_quint_trap(:) == 2))) ])  ]);
disp(['  Permanent Consumption Loss (%) | Q3(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 3)./cequiv_br(wealth_quint_init(:) == 3))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 3)./cequiv_br_trap(wealth_quint_init(:) == 3))), nanmean(log( cequiv_star_trap(wealth_quint_trap(:) == 3)./cequiv_br_trap(wealth_quint_trap(:) == 3))) ])  ]);
disp(['  Permanent Consumption Loss (%) | Q4(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 4)./cequiv_br(wealth_quint_init(:) == 4))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 4)./cequiv_br_trap(wealth_quint_init(:) == 4))), nanmean(log( cequiv_star_trap(wealth_quint_trap(:) == 4)./cequiv_br_trap(wealth_quint_trap(:) == 4))) ])  ]);
disp(['  Permanent Consumption Loss (%) | Q5(a)                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(wealth_quint_init(:) == 5)./cequiv_br(wealth_quint_init(:) == 5))),nanmean(log( cequiv_star_trap(wealth_quint_init(:) == 5)./cequiv_br_trap(wealth_quint_init(:) == 5))), nanmean(log( cequiv_star_trap(wealth_quint_trap(:) == 5)./cequiv_br_trap(wealth_quint_trap(:) == 5))) ])  ]);
disp( '                                                            ');
disp( '                                                            All  In-trap   Not-trap  ');
disp(['  Permanent Consumption Loss (%)    < 10                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(life_length(:) < 10)./cequiv_br(life_length(:) < 10))),nanmean(log( cequiv_star_trap(life_length(:) < 10)./cequiv_br_trap(life_length(:) < 10))), nanmean(log( cequiv_star_notrap(life_length(:) < 10)./cequiv_br_notrap(life_length(:) < 10)))])  ]);
disp(['  Permanent Consumption Loss (%)    < 50                  :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(life_length(:) < 50)./cequiv_br(life_length(:) < 50))),nanmean(log( cequiv_star_trap(life_length(:) < 50)./cequiv_br_trap(life_length(:) < 50))), nanmean(log( cequiv_star_notrap(life_length(:) < 50)./cequiv_br_notrap(life_length(:) < 50)))])  ]);
disp(['  Permanent Consumption Loss (%)    > 100                 :' , sprintf('%1.4f   ', 100*[nanmean(log( cequiv_star(life_length(:) > 100)./cequiv_br(life_length(:) > 100))),nanmean(log( cequiv_star_trap(life_length(:) > 100)./cequiv_br_trap(life_length(:) > 100))), nanmean(log( cequiv_star_notrap(life_length(:) > 100)./cequiv_br_notrap(life_length(:) > 100)))])  ]);


%% Ergodic policy functions 

%%% Not Normalized, simple average 

chat_pol_avg = nanmean(nanmean( chat_pol_sims, 3),2); 
chat_pol_avg_trap = NaN(size(chat_pol_avg,1), size(chat_pol_sims,2)); 
chat_pol_avg_notrap = chat_pol_avg_trap; 

for j = 1:size(chat_pol_sims,2)
    
    chat_pol_avg_trap(:,j)   = mean(chat_pol_sims(:,j,cross_alpha0( j + sample_half - size(chat_pol_sims,2),:) == 1),3); 
    chat_pol_avg_notrap(:,j) = mean(chat_pol_sims(:,j,cross_alpha0( j + sample_half - size(chat_pol_sims,2),:) == 0),3); 

end

chat_pol_avg_trap = mean( chat_pol_avg_trap,2); 
chat_pol_avg_notrap = mean( chat_pol_avg_notrap,2); 


plot(stateGrid_cross, [chat_pol_avg, cstar_brsim(stateGrid_cross)'])
%plot(stateGrid_cross , squeeze(chat_pol_sims(:,1,:)))

chat_pol_aquint_avg = NaN( size(chat_pol_sims,1), size(chat_pol_sims,2),5); 
chat_pol_htm_avg = NaN( size(chat_pol_sims,1), size(chat_pol_sims,2),2); 
chat_pol_aterc_avg = NaN( size(chat_pol_sims,1), size(chat_pol_sims,2),3);  

%shat_pol_aquint_avg = NaN( size(chat_pol_sims,1), size(chat_pol_sims,2),5); 


for j = 1:size(chat_pol_sims,2)
    
    quint_temp = prctile( a_sims_br(j +  sim_periods - size(chat_pol_sims,2),:), 100*[0.2, 0.4, 0.6, 0.8]);
    htm_ind_temp = htm_sims_br( j + sample_half - size(chat_pol_sims,2),:) == 1;

    chat_pol_aquint_avg(:,j,1) =  mean(chat_pol_sims(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    chat_pol_aquint_avg(:,j,2) =  mean(chat_pol_sims(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    chat_pol_aquint_avg(:,j,3) =  mean(chat_pol_sims(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    chat_pol_aquint_avg(:,j,4) =  mean(chat_pol_sims(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    chat_pol_aquint_avg(:,j,5) =  mean(chat_pol_sims(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    %shat_pol_aquint_avg(:,j,1) =  mean(shat_pol_sims(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);

    chat_pol_htm_avg(:,j,1) =  mean(chat_pol_sims(:,j, htm_ind_temp ==1 ),3);
    chat_pol_htm_avg(:,j,2) =  mean(chat_pol_sims(:,j, htm_ind_temp ==0 ),3);

    temp_ind    =        yi(j + sim_periods - size(chat_pol_sims,2),:) > scutoff; 
    aterc_temp  = prctile( a_sims_br(j + sim_periods - size(chat_pol_sims,2),:), 100*[0.3333, 0.666]); 
    
    aterc1_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) < aterc_temp(1); 
    aterc2_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) > aterc_temp(1) & a_sims_br(j + sim_periods - size(chat_pol_sims,2) -1,:) < aterc_temp(2); 
    aterc3_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) > aterc_temp(2); 

    chat_pol_aterc_avg(:,j,1) =  mean(chat_pol_sims(:,j, aterc1_ind_temp ==1 ),3);
    chat_pol_aterc_avg(:,j,2) =  mean(chat_pol_sims(:,j, aterc2_ind_temp ==1 ),3);
    chat_pol_aterc_avg(:,j,3) =  mean(chat_pol_sims(:,j, aterc3_ind_temp ==1 ),3);

end

% plot(stateGrid_cross(301:431) , [nanmean(chat_pol_aquint_avg((301:431),:,5),2) , cstar_brsim(stateGrid_cross(301:431) )'])
%plot(stateGrid_cross , [nanmean(chat_pol_aquint_avg(:,:,5),2) , cstar_brsim(stateGrid_cross)'])
%plot(stateGrid_cross , [nanmean(chat_pol_htm_avg(:,:,2),2) , cstar_brsim(stateGrid_cross)'])



%%
%%% Normalized 

chat_pol_norm        = NaN(size(chat_pol_sims)); 

norm_grid_nodes      = 51; 
chat_pol_norm        = NaN( norm_grid_nodes, size(chat_pol_sims,2), size(chat_pol_sims,3)); 
rw_pol_norm          = chat_pol_norm; 
cstar_pol_norm       = chat_pol_norm; 
constr_norm          = chat_pol_norm; 

chat_pol_norm_trap        = NaN( norm_grid_nodes, size(chat_pol_sims,2), size(chat_pol_sims,3)); 
rw_pol_norm_trap          = chat_pol_norm; 
cstar_pol_norm_trap       = chat_pol_norm; 

chat_pol_norm_notrap        = NaN( norm_grid_nodes, size(chat_pol_sims,2), size(chat_pol_sims,3)); 
rw_pol_norm_notrap          = chat_pol_norm; 
cstar_pol_norm_notrap       = chat_pol_norm; 

for j = 1:size(chat_pol_sims,2)
    for i = 1:nsim
        %cross_ind_temp = cross_ind_sims(j + sim_periods - size(chat_pol_sims,2),i);
        [~,cross_ind_temp] = min( abs(  ya(j + sim_periods - size(chat_pol_sims,2),i) - stateGrid_cross));
        
        if  cross_ind_temp > (norm_grid_nodes - 1)/2 && ( size(chat_pol_sims,1) - cross_ind_temp  > (norm_grid_nodes - 1)/2 )
            
            chat_pol_norm(:,j,i)  = chat_pol_sims( cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2 ,j,i); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            rw_pol_norm(:,j,i)    = rw_pol_interp( stateGrid_cross(cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2)); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            cstar_pol_norm(:,j,i) = cstar_brsim( stateGrid_cross(cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2)); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            constr_norm(:,j,i)    = stateGrid_cross(cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2);
            
            if cross_alpha0( j + sample_half - size(chat_pol_sims,2),i) == 1
                chat_pol_norm_trap(:,j,i)  = chat_pol_sims( cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2 ,j,i);% - rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
                rw_pol_norm_trap(:,j,i)    = rw_pol_interp( stateGrid_cross(cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2));% - rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
                cstar_pol_norm_trap(:,j,i) = cstar_brsim( stateGrid_cross(cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2));% - rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            end
            
            if cross_alpha0( j + sample_half - size(chat_pol_sims,2),i) == 0
                chat_pol_norm_notrap(:,j,i)  = chat_pol_sims( cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2 ,j,i);% - rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
                rw_pol_norm_notrap(:,j,i)    = rw_pol_interp( stateGrid_cross(cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2));% - rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
                cstar_pol_norm_notrap(:,j,i) = cstar_brsim( stateGrid_cross(cross_ind_temp - (norm_grid_nodes - 1)/2: cross_ind_temp + (norm_grid_nodes - 1)/2));% - rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            end
            
        else
            
            chat_pol_temp = griddedInterpolant(stateGrid_cross, chat_pol_sims(:,j,i),'linear','linear');
            temp_grid = linspace(stateGrid_cross( cross_ind_temp) - ( stateGrid_cross(2) - stateGrid_cross(1))*25,stateGrid_cross( cross_ind_temp) + ( stateGrid_cross(2) - stateGrid_cross(1))*25,51);
            
            chat_pol_norm(:,j,i)  =  chat_pol_temp( temp_grid);
            rw_pol_norm(:,j,i)    =  rw_pol_interp( temp_grid); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            cstar_pol_norm(:,j,i) =  cstar_brsim( temp_grid ); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            constr_norm(:,j,i)    =  temp_grid;

            if cross_alpha0( j + sample_half - size(chat_pol_sims,2),i) == 1
                chat_pol_norm(:,j,i)  =  chat_pol_temp( temp_grid);
                rw_pol_norm(:,j,i)    =  rw_pol_interp( temp_grid); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
                cstar_pol_norm(:,j,i) =  cstar_brsim( temp_grid ); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            end
            
            if cross_alpha0( j + sample_half - size(chat_pol_sims,2),i) == 0
                chat_pol_norm(:,j,i)  =  chat_pol_temp( temp_grid);
                rw_pol_norm(:,j,i)    =  rw_pol_interp( temp_grid); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
                cstar_pol_norm(:,j,i) =  cstar_brsim( temp_grid ); %- rw_pol_interp(ya_cross(j + sim_periods - size(chat_pol_sims,2),i));
            end
            
            
        end
    end
end

chat_pol_norm_avg = nanmean(nanmean( chat_pol_norm, 3),2); 
rw_pol_norm_avg   = nanmean(nanmean( rw_pol_norm,3),2); 
cstar_pol_norm_avg   = nanmean(nanmean( cstar_pol_norm,3),2); 

stateGrid_norm = stateGrid_cross(1:norm_grid_nodes) - stateGrid_cross( (norm_grid_nodes  + 1)/2);
% 
% plot(stateGrid_norm, [squeeze(chat_pol_norm(:,1,:))])
% plot(stateGrid_norm, [ chat_pol_norm_avg, rw_pol_norm_avg, cstar_pol_norm_avg])

chat_pol_aquint_norm    = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 
rw_pol_aquint_norm      = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 
cstar_pol_aquint_norm   = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 

chat_pol_htm_norm    = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 
rw_pol_htm_norm      = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 
cstar_pol_htm_norm   = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 
constr_htm_norm      = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 

chat_pol_aterc_norm    = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 
rw_pol_aterc_norm      = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 
cstar_pol_aterc_norm   = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 
constr_aterc_norm      = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),2 ); 

chat_pol_aquint_norm_trap    = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 
rw_pol_aquint_norm_trap      = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 
cstar_pol_aquint_norm_trap   = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 

chat_pol_aquint_norm_notrap    = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 
rw_pol_aquint_norm_notrap      = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 
cstar_pol_aquint_norm_notrap   = NaN(size(chat_pol_norm,1), size(chat_pol_norm,2),5 ); 

for j = 1:size(chat_pol_sims,2)
    
    quint_temp = prctile( a_sims_br(j +  sim_periods - size(chat_pol_sims,2),:), 100*[0.2, 0.4, 0.6, 0.8]);
    htm_ind_temp = htm_sims_br( j + sample_half - size(chat_pol_sims,2),:) == 1;
    

    temp_ind =                    yi(j + sim_periods - size(chat_pol_sims,2),:) > scutoff; 
    aterc_temp  = prctile( a_sims_br(j + sim_periods - size(chat_pol_sims,2),:), 100*[0.333,0.666]); 
    
    aterc1_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) < aterc_temp(1); 
    aterc2_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) > aterc_temp(1) & a_sims_br(j + sim_periods - size(chat_pol_sims,2) -1,:) < aterc_temp(2); 
    aterc3_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) > aterc_temp(2); 

    aterc1bigs_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) < aterc_temp(1) & temp_ind == 1; 
    aterc2bigs_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) > aterc_temp(1) & a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) < aterc_temp(2) & temp_ind == 1; 
    aterc3bigs_ind_temp = a_sims_br(j + sim_periods - size(chat_pol_sims,2) ,:) > aterc_temp(2) & temp_ind == 1; 
    
    chat_pol_aquint_norm(:,j,1) =  nanmean(chat_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    chat_pol_aquint_norm(:,j,2) =  nanmean(chat_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    chat_pol_aquint_norm(:,j,3) =  nanmean(chat_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    chat_pol_aquint_norm(:,j,4) =  nanmean(chat_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    chat_pol_aquint_norm(:,j,5) =  nanmean(chat_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    rw_pol_aquint_norm(:,j,1) =  nanmean(rw_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    rw_pol_aquint_norm(:,j,2) =  nanmean(rw_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    rw_pol_aquint_norm(:,j,3) =  nanmean(rw_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    rw_pol_aquint_norm(:,j,4) =  nanmean(rw_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    rw_pol_aquint_norm(:,j,5) =  nanmean(rw_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    cstar_pol_aquint_norm(:,j,1) =  nanmean(cstar_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    cstar_pol_aquint_norm(:,j,2) =  nanmean(cstar_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    cstar_pol_aquint_norm(:,j,3) =  nanmean(cstar_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    cstar_pol_aquint_norm(:,j,4) =  nanmean(cstar_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    cstar_pol_aquint_norm(:,j,5) =  nanmean(cstar_pol_norm(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    chat_pol_aquint_norm_trap(:,j,1) =  nanmean(chat_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    chat_pol_aquint_norm_trap(:,j,2) =  nanmean(chat_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    chat_pol_aquint_norm_trap(:,j,3) =  nanmean(chat_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    chat_pol_aquint_norm_trap(:,j,4) =  nanmean(chat_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    chat_pol_aquint_norm_trap(:,j,5) =  nanmean(chat_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    rw_pol_aquint_norm_trap(:,j,1) =  nanmean(rw_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    rw_pol_aquint_norm_trap(:,j,2) =  nanmean(rw_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    rw_pol_aquint_norm_trap(:,j,3) =  nanmean(rw_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    rw_pol_aquint_norm_trap(:,j,4) =  nanmean(rw_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    rw_pol_aquint_norm_trap(:,j,5) =  nanmean(rw_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    cstar_pol_aquint_norm_trap(:,j,1) =  nanmean(cstar_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    cstar_pol_aquint_norm_trap(:,j,2) =  nanmean(cstar_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    cstar_pol_aquint_norm_trap(:,j,3) =  nanmean(cstar_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    cstar_pol_aquint_norm_trap(:,j,4) =  nanmean(cstar_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    cstar_pol_aquint_norm_trap(:,j,5) =  nanmean(cstar_pol_norm_trap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    chat_pol_aquint_norm_notrap(:,j,1) =  nanmean(chat_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    chat_pol_aquint_norm_notrap(:,j,2) =  nanmean(chat_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    chat_pol_aquint_norm_notrap(:,j,3) =  nanmean(chat_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    chat_pol_aquint_norm_notrap(:,j,4) =  nanmean(chat_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    chat_pol_aquint_norm_notrap(:,j,5) =  nanmean(chat_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    rw_pol_aquint_norm_notrap(:,j,1) =  nanmean(rw_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    rw_pol_aquint_norm_notrap(:,j,2) =  nanmean(rw_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    rw_pol_aquint_norm_notrap(:,j,3) =  nanmean(rw_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    rw_pol_aquint_norm_notrap(:,j,4) =  nanmean(rw_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    rw_pol_aquint_norm_notrap(:,j,5) =  nanmean(rw_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    cstar_pol_aquint_norm_notrap(:,j,1) =  nanmean(cstar_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <=quint_temp(1) ),3);
    cstar_pol_aquint_norm_notrap(:,j,2) =  nanmean(cstar_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(1) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(2)   ) ,3);
    cstar_pol_aquint_norm_notrap(:,j,3) =  nanmean(cstar_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(2) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(3)   ) ,3);
    cstar_pol_aquint_norm_notrap(:,j,4) =  nanmean(cstar_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(3) &  a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) <= quint_temp(4)   ) ,3);
    cstar_pol_aquint_norm_notrap(:,j,5) =  nanmean(cstar_pol_norm_notrap(:,j, a_sims_br(j + sim_periods - size(chat_pol_sims,2),:) > quint_temp(4) ),3 ); 
    
    chat_pol_htm_norm(:,j,1) =  nanmean(chat_pol_norm(:,j, htm_ind_temp == 1 ),3);
    rw_pol_htm_norm(:,j,1) =  nanmean(rw_pol_norm(:,j, htm_ind_temp == 1 ),3);
    cstar_pol_htm_norm(:,j,1) =  nanmean(cstar_pol_norm(:,j, htm_ind_temp == 1 ),3);
    constr_htm_norm(:,j,1)   =   nanmean(constr_norm(:,j, htm_ind_temp == 1),3); 
    
    chat_pol_htm_norm(:,j,2) =  nanmean(chat_pol_norm(:,j, htm_ind_temp == 0 ),3);
    rw_pol_htm_norm(:,j,2) =  nanmean(rw_pol_norm(:,j, htm_ind_temp == 0 ),3);
    cstar_pol_htm_norm(:,j,2) =  nanmean(cstar_pol_norm(:,j, htm_ind_temp == 0 ),3);
    constr_htm_norm(:,j,2)   =   nanmean(constr_norm(:,j, htm_ind_temp == 0),3); 

    chat_pol_aterc_norm(:,j,1)  =  nanmean(chat_pol_norm(:,j, aterc1_ind_temp == 1 ),3);
    rw_pol_aterc_norm(:,j,1)    =  nanmean(rw_pol_norm(:,j, aterc1_ind_temp == 1 ),3);
    cstar_pol_aterc_norm(:,j,1) =  nanmean(cstar_pol_norm(:,j, aterc1_ind_temp == 1 ),3);
    constr_aterc_norm(:,j,1)    =   nanmean(constr_norm(:,j, aterc1_ind_temp == 1),3); 
    
    chat_pol_aterc_norm(:,j,2)  =  nanmean(chat_pol_norm(:,j, aterc2_ind_temp == 1 ),3);
    rw_pol_aterc_norm(:,j,2)    =  nanmean(rw_pol_norm(:,j, aterc2_ind_temp == 1 ),3);
    cstar_pol_aterc_norm(:,j,2) =  nanmean(cstar_pol_norm(:,j, aterc2_ind_temp == 1 ),3);
    constr_aterc_norm(:,j,2)    =  nanmean(constr_norm(:,j, aterc2_ind_temp == 1),3); 
    
    chat_pol_aterc_norm(:,j,3)  =  nanmean(chat_pol_norm(:,j, aterc3_ind_temp == 1 ),3);
    rw_pol_aterc_norm(:,j,3)    =  nanmean(rw_pol_norm(:,j, aterc3_ind_temp == 1 ),3);
    cstar_pol_aterc_norm(:,j,3) =  nanmean(cstar_pol_norm(:,j, aterc3_ind_temp == 1 ),3);
    constr_aterc_norm(:,j,3)    =  nanmean(constr_norm(:,j, aterc3_ind_temp == 1),3); 
end

figure
subplot(3,2,1)
plot(stateGrid_cross, [chat_pol_avg , cstar_brsim(stateGrid_cross )'])
subplot(3,2,2)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm(:,:,1),2),nanmean(rw_pol_aquint_norm(:,:,1),2),nanmean(cstar_pol_aquint_norm(:,:,1),2)])
subplot(3,2,3)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm(:,:,2),2),nanmean(rw_pol_aquint_norm(:,:,2),2),nanmean(cstar_pol_aquint_norm(:,:,2),2)])
subplot(3,2,4)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm(:,:,3),2),nanmean(rw_pol_aquint_norm(:,:,3),2),nanmean(cstar_pol_aquint_norm(:,:,3),2)])
subplot(3,2,5)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm(:,:,4),2),nanmean(rw_pol_aquint_norm(:,:,4),2),nanmean(cstar_pol_aquint_norm(:,:,4),2)])
subplot(3,2,6)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm(:,:,5),2),nanmean(rw_pol_aquint_norm(:,:,5),2),nanmean(cstar_pol_aquint_norm(:,:,5),2)])


ya_end_ind = NaN(nsim,1); 

for i = 1:nsim
      
    [~,ya_end_ind(i)] = min( abs( ya_end(i) - stateGrid_cross)); 
    
    
end

chat_polpredict = diag( squeeze(  chat_pol_sims(ya_end_ind ,end,:)) );
diff_temp2 =  cstar_brsim( ya_end)'  - chat_polpredict;

chat_polpredict = min(  [chat_polpredict, ya_end' - b],[],2); 
diff_temp  = chat_sims_br(end,:) - chat_polpredict';

diff_temp3 =  min([cstar_brsim( ya_end)', ya_end' - b],[],2)  - chat_polpredict;

avg_poldiff = NaN( size(chat_pol_aquint_norm,1), 5); 
avg_poldiff(:,1) = nanmean(chat_pol_aquint_norm(:,:,1),2) - nanmean(cstar_pol_aquint_norm(:,:,1),2);
avg_poldiff(:,2) = nanmean(chat_pol_aquint_norm(:,:,2),2) - nanmean(cstar_pol_aquint_norm(:,:,2),2);
avg_poldiff(:,3) = nanmean(chat_pol_aquint_norm(:,:,3),2) - nanmean(cstar_pol_aquint_norm(:,:,3),2);
avg_poldiff(:,4) = nanmean(chat_pol_aquint_norm(:,:,4),2) - nanmean(cstar_pol_aquint_norm(:,:,4),2);
avg_poldiff(:,5) = nanmean(chat_pol_aquint_norm(:,:,5),2) - nanmean(cstar_pol_aquint_norm(:,:,5),2);

%% In and out of trap splits

figure
subplot(3,2,1)
plot(stateGrid_cross, [chat_pol_avg_trap,  cstar_brsim(stateGrid_cross )'])
subplot(3,2,2)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_trap(:,:,1),2),nanmean(rw_pol_aquint_norm_trap(:,:,1),2),nanmean(cstar_pol_aquint_norm_trap(:,:,1),2)])
subplot(3,2,3)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_trap(:,:,2),2),nanmean(rw_pol_aquint_norm_trap(:,:,2),2),nanmean(cstar_pol_aquint_norm_trap(:,:,2),2)])
subplot(3,2,4)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_trap(:,:,3),2),nanmean(rw_pol_aquint_norm_trap(:,:,3),2),nanmean(cstar_pol_aquint_norm_trap(:,:,3),2)])
subplot(3,2,5)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_trap(:,:,4),2),nanmean(rw_pol_aquint_norm_trap(:,:,4),2),nanmean(cstar_pol_aquint_norm_trap(:,:,4),2)])
subplot(3,2,6)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_trap(:,:,5),2),nanmean(rw_pol_aquint_norm_trap(:,:,5),2),nanmean(cstar_pol_aquint_norm_trap(:,:,5),2)])


figure
subplot(3,2,1)
plot(stateGrid_cross, [chat_pol_avg_notrap  , cstar_brsim(stateGrid_cross )'])
subplot(3,2,2)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_notrap(:,:,1),2),nanmean(rw_pol_aquint_norm_notrap(:,:,1),2),nanmean(cstar_pol_aquint_norm_notrap(:,:,1),2)])
subplot(3,2,3)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_notrap(:,:,2),2),nanmean(rw_pol_aquint_norm_notrap(:,:,2),2),nanmean(cstar_pol_aquint_norm_notrap(:,:,2),2)])
subplot(3,2,4)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_notrap(:,:,3),2),nanmean(rw_pol_aquint_norm_notrap(:,:,3),2),nanmean(cstar_pol_aquint_norm_notrap(:,:,3),2)])
subplot(3,2,5)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_notrap(:,:,4),2),nanmean(rw_pol_aquint_norm_notrap(:,:,4),2),nanmean(cstar_pol_aquint_norm_notrap(:,:,4),2)])
subplot(3,2,6)
plot(stateGrid_norm, [nanmean(chat_pol_aquint_norm_notrap(:,:,5),2),nanmean(rw_pol_aquint_norm_notrap(:,:,5),2),nanmean(cstar_pol_aquint_norm_notrap(:,:,5),2)])
%% Ergodic Policy pictures

figure 
plot(stateGrid_cross, [chat_pol_avg , cstar_brsim(stateGrid_cross )'], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash on hand (y)')
ylabel('Consumption (c)')
xlim([0 30])
legend('Avg policy estimate: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -depsc avg_policy.eps

figure 
plot(stateGrid_cross, [nanmean(chat_pol_htm_avg(:,:,1),2), nanmean(chat_pol_htm_avg(:,:,2),2) , cstar_brsim(stateGrid_cross )'], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash on hand (y)')
ylabel('Consumption (c)')
xlim([0 20])
legend('Avg policy estimate: $\int \hat c_i(y) | HtM $', 'Avg policy estimate: $\int \hat c_i(y) | not-HtM $', 'Full info: $c^*(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -depsc avg_policy_htm.eps
 
figure 
plot(stateGrid_norm,[nanmean(chat_pol_htm_norm(:,:,1),2),nanmean(cstar_pol_htm_norm(:,:,1),2),nanmean(rw_pol_htm_norm(:,:,1),2)], 'LineWidth', 2.5); hold on
plot( stateGrid_norm,nanmean(constr_htm_norm(:,:,1),2),':','LineWidth',1.5)
ax = gca;
ax.FontSize = 15;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
ylim([0.7 1.8])
legend('Avg effective policy: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','Constraint: MPC = 1','location','Northwest','interpreter','latex','FontSize',15)

print -depsc norm_policy_htm.eps

figure 
plot(stateGrid_norm,[nanmean(chat_pol_htm_norm(:,:,2),2),nanmean(cstar_pol_htm_norm(:,:,2),2),nanmean(rw_pol_htm_norm(:,:,2),2)], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 15;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg effective policy: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -depsc norm_policy_nothtm.eps

figure 
plot(stateGrid_norm,[nanmean(chat_pol_aquint_norm(:,:,1),2),nanmean(cstar_pol_aquint_norm(:,:,1),2),nanmean(rw_pol_aquint_norm(:,:,1),2)], 'LineWidth', 2.5); hold on
plot( stateGrid_norm,nanmean(constr_htm_norm(:,:,1),2),':','LineWidth',1.5)
ax = gca;
ax.FontSize = 15;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
ylim([0.7 1.8])
legend('Avg effective policy: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','Constraint: MPC = 1','location','Northwest','interpreter','latex','FontSize',15)

print -depsc norm_policy_q1.eps

figure 
plot(stateGrid_norm,[nanmean(chat_pol_aquint_norm(:,:,2),2),nanmean(cstar_pol_aquint_norm(:,:,2),2),nanmean(rw_pol_aquint_norm(:,:,2),2)], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg effective policy: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -depsc norm_policy_q2.eps

figure 
plot(stateGrid_norm,[nanmean(nanmean(chat_pol_aquint_norm(:,:,3),3),2),nanmean(nanmean(cstar_pol_aquint_norm(:,:,3),3),2),nanmean(nanmean(rw_pol_aquint_norm(:,:,3),2),3)], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg effective policy: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -depsc norm_policy_q3.eps

figure 
plot(stateGrid_norm,[nanmean(nanmean(chat_pol_aquint_norm(:,:,4),3),2),nanmean(nanmean(cstar_pol_aquint_norm(:,:,4),3),2),nanmean(nanmean(rw_pol_aquint_norm(:,:,4),2),3)], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg effective policy: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -depsc norm_policy_q4.eps

figure 
plot(stateGrid_norm,[nanmean(nanmean(chat_pol_aquint_norm(:,:,5),3),2),nanmean(nanmean(cstar_pol_aquint_norm(:,:,5),3),2),nanmean(nanmean(rw_pol_aquint_norm(:,:,5),2),3)], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg effective policy: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -depsc norm_policy_q5.eps

%% Normalized policies with the constraint

ind_temp = find(nanmean(rw_pol_aterc_norm(:,:,1),2) - nanmean(constr_aterc_norm(:,:,1),2) < 0, 1, 'first');

figure 
plot(stateGrid_norm(7:end-2),[nanmean(chat_pol_aterc_norm(3:end-6,:,1),2),nanmean(cstar_pol_aterc_norm(4:end-5,:,1),2),nanmean(rw_pol_aterc_norm(4:end-5,:,1),2),nanmean(constr_aterc_norm(2:end-7,:,1),2) ], 'LineWidth', 2.5)
vline([ stateGrid_norm(ind_temp + 12)], ':r', '$\overline{y}_{S,2}$', 14, 1, 0.35)
ylim([ 0.9,1.6])
xlim([-1 , 1.15])
ax = gca;
ax.FontSize = 13;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg policy estimate: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -dpdf norm_policy_aterc1.pdf

figure 
plot(stateGrid_cross, nanmean(chat_pol_aterc_avg(:,:,1),2) + 0.1, '-b','LineWidth',2)
hold on 
plot(stateGrid_cross, cstar_brsim(stateGrid_cross )','-r','LineWidth',2)
plot(stateGrid_cross, rw_pol_cross,'-.k','LineWidth',1.25)
scatter( stateGrid_cross(39),nanmean(chat_pol_aterc_avg(39,:,1),2) + 0.1,200,'b','filled')
scatter( stateGrid_cross(39),cstar_brsim(stateGrid_cross(39)),200,'r','filled')
scatter( stateGrid_cross(29),stateGrid_cross(29),200,'b')
scatter( stateGrid_cross(38),cstar_brsim(stateGrid_cross(38)),200,'r')
plot(stateGrid_cross, stateGrid_cross' - b,':k','LineWidth',1.25)
ax = gca;
ax.FontSize = 10;
xlabel('Cash on hand (y)')
ylabel('Consumption (c)')
xlim([0.5 4])
ylim([0.8 1.75])
legend('Avg policy estimate: $\int \widehat c_i(y) |$ Low $a_{t-1}$ ', 'Full info: $c^*(y)$','RW-y policy: $c^{RW}(y)$','Time-$t$ action: $c_t$','Time-$t$ opt. action: $ c_t^*$','Time-$t+1$ action: $c_{t+1}$','Time-$t$ opt. action: $ c_{t+1}^*$','location','Southeast','interpreter','latex','FontSize',12)

print -depsc avg_policy_aterc1.eps


ind_temp = find(nanmean(rw_pol_aterc_norm(:,:,2),2) - nanmean(chat_pol_aterc_norm(:,:,2),2) < 0, 1, 'first');

figure 
plot(stateGrid_norm,[nanmean(chat_pol_aterc_norm(:,:,2),2),nanmean(cstar_pol_aterc_norm(:,:,2),2),nanmean(rw_pol_aterc_norm(:,:,2),2) ], 'LineWidth', 2.5)
vline([ stateGrid_norm(ind_temp + 11)], ':r', '$\overline{y}_{S,2}$', 14, 1, 0.35)
ax = gca;
ax.FontSize = 13;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg policy estimate: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -dpdf norm_policy_aterc2.pdf


ind_temp = find(nanmean(rw_pol_aterc_norm(:,:,3),2) - nanmean(chat_pol_aterc_norm(:,:,3),2) < 0, 1, 'first');

figure 
plot(stateGrid_norm,[nanmean(chat_pol_aterc_norm(:,:,3),2),nanmean(cstar_pol_aterc_norm(:,:,3),2),nanmean(rw_pol_aterc_norm(:,:,3),2) ], 'LineWidth', 2.5)
vline([ stateGrid_norm(ind_temp + 11)], ':r', '$\overline{y}_{S,2}$', 14, 1, 0.35)
ax = gca;
ax.FontSize = 13;
xlabel('Cash-on-hand (Deviation from $y_i$)', 'interpreter','latex')
ylabel('Consumption (c)')
%xlim([0 30])
legend('Avg policy estimate: $\int \hat c_i(y)$', 'Full info: $c^*(y)$','RW-wealth policy: $c^{RW}(y)$','location','Northwest','interpreter','latex','FontSize',15)

print -dpdf norm_policy_aterc3.pdf
%%

figure 
plot(stateGrid_cross, [nanmean(chat_pol_htm_avg(:,:,1),2), nanmean(chat_pol_htm_avg(:,:,2),2) , cstar_brsim(stateGrid_cross )', stateGrid_cross', rw_pol_cross], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash on hand (y)')
ylabel('Consumption (c)')
xlim([0 3])
ylim([0.6 2.2])
legend('Avg policy estimate: $\int \hat c_i(y) | HtM $', 'Avg policy estimate: $\int \hat c_i(y) | not-HtM $', 'Full info: $c^*(y)$','location','Northwest','interpreter','latex','FontSize',15)

figure 
plot(stateGrid_cross, [nanmean(chat_pol_aquint_avg(:,:,2),2), nanmean(chat_pol_htm_avg(:,:,2),2) , cstar_brsim(stateGrid_cross )', -1.1059 + w_br*stateGrid_cross', rw_pol_cross], 'LineWidth', 2.5)
ax = gca;
ax.FontSize = 13;
xlabel('Cash on hand (y)')
ylabel('Consumption (c)')
xlim([0 20])
ylim([0.6 2.2])
legend('Avg policy estimate: $\int \hat c_i(y) | HtM $', 'Avg policy estimate: $\int \hat c_i(y) | not-HtM $', 'Full info: $c^*(y)$','location','Northwest','interpreter','latex','FontSize',15)

%% Test figures 

%%% interesting indeces 917, 651, 492

ind = 917; 

figure 
plot(stateGrid_cross, [chat_pol_sims(:,end,ind), chat_pol_sims(:,end-1,ind),cstar_brsim(stateGrid_cross)', rw_pol_interp(stateGrid_cross)', stateGrid_cross'])
xlim([0, 3])


figure 
plot(stateGrid_cross, [shat_pol_sims(:,end,ind),shat_pol_sims(:,end-1,ind)])
xlim([0, 3])


%% ybar_i disribution 

xi_ybar = linspace(min(ya_cross(:)), max(ya_cross(:)), 45); 
xi_ybar = sort( [xi_ybar, mean(xi_ybar(1:2))]);
 
h = histogram( ya_cross(:), xi_ybar,'EdgeColor', 'b','Normalization','probability','DisplayStyle','stairs')
ybari_pdf = h.Values./sum(h.Values); 
xi_ybar = h.BinEdges; 
xi_ybar = mean( [xi_ybar(1:end-1)', xi_ybar(2:end)'],2); 


fi_cross_ind= find( cstar_brsim(stateGrid_cross)' - rw_pol_cross > 0,1,'first');
if isempty(fi_cross_ind)
    fi_cross_ind =1;
end

figure
ax = axes;
%hold all 
plot(xi_ybar, ybari_pdf,'Color',[0.8500, 0.3250, 0.0980],'LineWidth',2.5)
xline(stateGrid_cross(fi_cross_ind),'-.','color',[0, 0.4470, 0.7410],'LineWidth',2.5)
xlim([min(xi_ybar)-0.5, 25])
ylim([0 0.24])
xlabel('Cash-on-hand (y)')
ylabel('Probability density')
%yticks([0,0.05,0.1,0.15,0.2])
legend('$\bar y_i^{BR}$','$\bar y_i^{FI}$','FontSize',15,'interpreter','latex')
myLines = get(ax,'Children');
% Change the order the data is plotted.
set(ax,'Children',myLines([2 1]));
legend('$\bar y_i^{FI}$','$\bar y_i^{BR}$','FontSize',15,'interpreter','latex')

%title('Wealth Distribution')
%ax = gca;
ax.FontSize = 12;

print -depsc ybari_dist.eps

%%

%{
temp = find(  a_sims_br(end,:) > quint_temp(3) &  a_sims_br(end,:) <= quint_temp(4)   & ~trap_ind_temp );

ind_temp = temp(1); 
[a_sims_br(end-age_sims_br(end,ind_temp)+1:end,ind_temp),avg_apc_temp(end-age_sims_br(end,ind_temp)+1:end,ind_temp), chat_temp(end-age_sims_br(end,ind_temp)+1:end,ind_temp)- crw_temp(end-age_sims_br(end,ind_temp)+1:end,ind_temp), yi(end-age_sims_br(end,ind_temp)+1:end,ind_temp), mpc_temp2(end-age_sims_br(end,ind_temp)+1:end,ind_temp)]
scatter(mpc_temp2(end-age_sims_br(end,ind_temp)+1:end,ind_temp),avg_apc_temp(end-age_sims_br(end,ind_temp)+1:end,ind_temp))
corr(mpc_temp2(end-age_sims_br(end,ind_temp)+1:end,ind_temp),avg_apc_temp(end-age_sims_br(end,ind_temp)+1:end,ind_temp));
%}
%%

quint_temp = prctile( a_sims_br(end,:), 100*[0.2, 0.4, 0.6, 0.8]);
%quint_temp(1) = quint_temp(2); 

subplot(1,2,1)    
scatter( mpc_temp2(end,:),avg_apc_temp(end,:))
subplot(1,2,2)
scatter( mpc_aquint(end,1:5), avg_apc_aquint(end,1:5))

subplot(1,2,1)
scatter( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1)),avg_apc_temp(end,a_sims_br(end,:) > quint_temp(1)))
subplot(1,2,2)
scatter( mpc_aquint(end,2:5), avg_apc_aquint(end,2:5))

[b_mpcapc,bint,~,~,stats]        = regress( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))', [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), avg_apc_temp(end,a_sims_br(end,:) > quint_temp(1))']);
[b_mpcapc_aquint,bint,~,~,stats] = regress( mpc_aquint(end,2:5)', [ones(length( avg_apc_aquint(end,2:5)),1), avg_apc_aquint(end,2:5)']);
[b_mpcapc_aquint,bint,~,~,stats] = regress( mpc_aquint(end,1:5)', [ones(length( avg_apc_aquint(end,1:5)),1), avg_apc_aquint(end,1:5)']);
[b_mpcapc,bint,~,~,stats]        = regress( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))', [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), avg_apc_temp(end,a_sims_br(end,:) > quint_temp(1))', a_sims_br(end, a_sims_br(end,:) > quint_temp(1))'+yi(end,a_sims_br(end,:) > quint_temp(1))']);

[b1,bint,~,~,stats]        = regress( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))', [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), a_sims_br(end, a_sims_br(end,:) > quint_temp(1))']);
[b2,bint,~,~,stats]        = regress( avg_apc_temp2(end,a_sims_br(end,:) > quint_temp(1))', [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), a_sims_br(end, a_sims_br(end,:) > quint_temp(1))']);

mpc_hat = [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), a_sims_br(end, a_sims_br(end,:) > quint_temp(1))']*b1;
apc_hat = [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), a_sims_br(end, a_sims_br(end,:) > quint_temp(1))']*b2;
apc_hat_e = avg_apc_temp(end,a_sims_br(end,:) > quint_temp(1))' - [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), a_sims_br(end, a_sims_br(end,:) > quint_temp(1))']*b2;

[b_mpcapc_hat,~,~,~,stats]        = regress( mpc_hat, [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), apc_hat]);
[b_mpcapc_hat,bint,~,~,stats]        = regress( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))', [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), apc_hat]);
[b_mpcapc_hat,bint,~,~,stats]        = regress( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))', [ones(length(mpc_temp2(end,a_sims_br(end,:) > quint_temp(1))),1), apc_hat_e, a_sims_br(end, a_sims_br(end,:) > quint_temp(1))']);


[b_mpcapc_alla,bint,~,~,stats]        = regress( mpc_temp2(end,: )', [ones(length(mpc_temp2(end,:)),1), avg_apc_temp(end,:  )']);
[b_mpcapc_alla,bint,~,~,stats]        = regress( mpc_temp2(end,: )', [ones(length(mpc_temp2(end,:)),1), avg_apc_temp(end,:  )', a_sims_br(end,:)', yi(end,:)']);
[b_mpcapc_alla,bint,~,~,stats]        = regress( mpc_temp2(end, unconstr_ind(end,:) == 1)', [ones(length(mpc_temp2(end,unconstr_ind(end,:) == 1)),1), avg_apc_temp(end, unconstr_ind(end,:) == 1  )', a_sims_br(end,unconstr_ind(end,:) == 1)', yi(end,unconstr_ind(end,:) == 1)']);
[b_mpcapc_alla,bint,~,~,stats]        = regress( mpc_temp2(end, unconstr_ind(end,:) == 1)', [ones(length(mpc_temp2(end,unconstr_ind(end,:) == 1)),1), avg_apc_temp(end, unconstr_ind(end,:) == 1  )', a_sims_br(end,unconstr_ind(end,:) == 1)']);

[b_mpcapc_alla,bint,~,~,stats]        = regress( mpc_temp2(end, a_sims_br(end,:) > 0)', [ones(length(mpc_temp2(end,a_sims_br(end,:) > 0)),1), avg_apc_temp(end, a_sims_br(end,:) > 0  )', a_sims_br(end,a_sims_br(end,:) > 0)', yi(end,a_sims_br(end,:) > 0)']);
[b_mpcapc_alla,bint,~,~,stats]        = regress( mpc_temp2(end, a_sims_br(end,:) > 0)', [ones(length(mpc_temp2(end,a_sims_br(end,:) > 0)),1), avg_apc_temp(end, a_sims_br(end,:) > 0  )', a_sims_br(end,a_sims_br(end,:) > 0)']);


temp1 = mpc_temp2( a_sims_br(sample_half+1:end,:) > 0);
temp2 = avg_apc_temp( a_sims_br(sample_half+1:end,:) > 0);
subplot(1,2,1)
scatter( temp1(:),temp2(:))
subplot(1,2,2)
scatter( nanmean(mpc_aquint(:,2:5)), nanmean(avg_apc_aquint(:,2:5)))


[b_mpcapc_alla,bint,~,~,stats]        = regress( mpc_temp2(:), [ones(length(mpc_temp2(:)),1), avg_apc_temp(:)]);
[b_mpcapc_alla,bint,~,~,stats]        = regress( temp1(:), [ones(length(temp1(:)),1), temp2(:)]);


temp1 = mpc_aquint(:,2:5); 
temp2 = avg_apc_aquint(:,2:5); 
scatter( temp1(:), temp2(:))
[b_mpcapc_aquint,bint,~,~,stats] = regress( temp1(:), [ones(length( temp1(:)),1), temp2(:)]);

%%% Within bins there is some negative correlation 
temp1 = [mpc_notrap_aquint(:,2:5),mpc_trap_aquint(:,2:5)]; 
temp2 = [avg_apc_notrap_aquint(:,2:5),avg_apc_trap_aquint(:,2:5)]; 
subplot(2,2,1)
scatter( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1)),avg_apc_temp(end,a_sims_br(end,:) > quint_temp(1)))
subplot(2,2,2)
scatter( mpc_aquint(end,2:5), avg_apc_aquint(end,2:5))
subplot(2,2,3)
scatter( [mpc_notrap_aquint(end,2:5),mpc_trap_aquint(end,2:5)]', [avg_apc_notrap_aquint(end,2:5),avg_apc_trap_aquint(end,2:5)]')
subplot(2,2,4)
scatter( temp1(:), temp2(:))

[b_mpcapc_trapaquint,bint,~,~,stats] = regress( [mpc_notrap_aquint(end,2:5),mpc_trap_aquint(end,2:5)]', [ones(length([avg_apc_notrap_aquint(end,2:5),avg_apc_trap_aquint(end,2:5)]'),1), [avg_apc_notrap_aquint(end,2:5),avg_apc_trap_aquint(end,2:5)]']);
[b_mpcapc_trapaquint,bint,~,~,stats] = regress( temp1(:), [ones(length(temp1(:)),1), temp2(:)]);


temp1 = [mpc_notrap_aquint(:,3),mpc_trap_aquint(:,3)]; 
temp2 = [avg_apc_notrap_aquint(:,3),avg_apc_trap_aquint(:,3)]; 
subplot(2,2,1)
scatter( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1)),avg_apc_temp(end,a_sims_br(end,:) > quint_temp(1)))
subplot(2,2,2)
scatter( mpc_aquint(end,2:5), avg_apc_aquint(end,2:5))
subplot(2,2,3)
scatter( [mpc_notrap_aquint(end,2:5),mpc_trap_aquint(end,2:5)]', [avg_apc_notrap_aquint(end,2:5),avg_apc_trap_aquint(end,2:5)]')
subplot(2,2,4)
scatter( temp1(:), temp2(:))

[b_mpcapc_trapaquint,bint,~,~,stats] = regress( temp1(:), [ones(length(temp1(:)),1), temp2(:)]);

temp1 = [mpc_notrap_aquint(:,5)]; 
temp2 = [avg_apc_notrap_aquint(:,5)]; 
subplot(2,2,1)
scatter( mpc_temp2(end,a_sims_br(end,:) > quint_temp(1)),avg_apc_temp(end,a_sims_br(end,:) > quint_temp(1)))
subplot(2,2,2)
scatter( mpc_aquint(end,2:5), avg_apc_aquint(end,2:5))
subplot(2,2,3)
scatter( [mpc_notrap_aquint(end,2:5)]', [avg_apc_notrap_aquint(end,2:5)]')
subplot(2,2,4)
scatter( temp1(:), temp2(:))

[b_mpcapc_trapaquint,bint,~,~,stats] = regress( temp1(:), [ones(length(temp1(:)),1), temp2(:)]);

%% Lagged income

scatter(yi(end-1,age_temp(end,:) >1 & a_sims_br(end,:) > 0)', avg_apc_temp(end,age_temp(end,:) >1 & a_sims_br(end,:) > 0)')
corr(yi(end-1,age_temp(end,:) >1 & a_sims_br(end,:) > 0)', avg_apc_temp(end,age_temp(end,:) >1 & a_sims_br(end,:) > 0)')
scatter(yi(end-1,age_temp(end,:) >1 & a_sims_br(end,:) > 0)', apc_temp(end,age_temp(end,:) >1 & a_sims_br(end,:) > 0)')
corr(yi(end-1,age_temp(end,:) >1 & a_sims_br(end,:) > 0)', apc_temp(end,age_temp(end,:) >1 & a_sims_br(end,:) > 0)')
corr(mean(yi(end-10:end-1,age_temp(end,:) >1 & a_sims_br(end,:) > 0))', apc_temp(end,age_temp(end,:) >1 & a_sims_br(end,:) > 0)')

corr(mean(yi(end-4:end-1,age_temp(end,:) >4 & a_sims_br(end,:) > 0))', apc_temp(end,age_temp(end,:) >4 & a_sims_br(end,:) > 0)')
corr(mean(yi(end-10:end-1,age_temp(end,:) > 10 & a_sims_br(end,:) > 0) + rt_br*a_sims_br(end-10:end-1,age_temp(end,:) > 10 & a_sims_br(end,:) > 0))', apc_temp(end,age_temp(end,:) > 10 & a_sims_br(end,:) > 0)')

corr(mean(yi(end-4:end-1,age_temp(end,:) >4 & a_sims_br(end,:) > 0))', mpc_temp(end,age_temp(end,:) >4 & a_sims_br(end,:) > 0)')

%%

%{
test_ind = [find(unconstr_ind(end,:) == 1& age_temp(end,:) == 1)];
y_mpc=yi(end,test_ind)*(1 + factor_mpc1);
%y_mpc=yi(end,520);

[~,~,chat_mpc, eta_t, ~, ~, ~, ~, ~] = bc_update_iid_fast(rt_br, W_cc, kappa, w_br*y_mpc', a_temp(end,test_ind)', sigma_c, psi, cov_fun, 0, eps_etai(end,test_ind)',  b, [], [],[],[], ctilde, stateGrid,stateGrid_step,stateGrid_size, log_c);
mpc_test=(chat_mpc-c_temp(end,test_ind))./(w_br*factor_mpc1);


%eta_temp =  fast_interp( w_br*y_mpc + (1+rt_br)*a_temp(end,test_ind), ctilde, stateGrid_step, stateGrid,stateGrid_size) + eps_etai(end,test_ind)*sqrt(sigma_etasq_sims(end,test_ind));
%eta_sims(end,test_ind);

apc_sims     = chat_sims_br./( rt_br*a_sims_br + w_br*yi);

ind_min = 218;
ind_max = 124;

eta_temp =  eta_sims(1,ind_min);
[chat_polmin,shat_pol] =  gp_update(stateGrid', yi(1,ind_min)*w_br + a_sims_br(1,ind_min)*(1+r_br), eta_temp, sigma_etasq_sims(1,ind_min), mu0_fun,sigma_c,psi, cov_fun);
eta_temp =  eta_sims(1,ind_max);
[chat_polmax,shat_pol] =  gp_update(stateGrid', yi(1,ind_max)*w_br + a_sims_br(1,ind_max)*(1+r_br), eta_temp, sigma_etasq_sims(1,ind_max), mu0_fun,sigma_c,psi, cov_fun);
%}

%% HTM status transitions

reset_ind1 = reset_shocks( sample_half+1:end-1,:) < deltax;
reset_ind2 = reset_shocks( sample_half+2:end,:) < deltax;


total_HTM = double(htm_sims_br(1:end-2,:));
total_HTM(reset_ind1 == 1) = NaN;
total_HTM(reset_ind2 == 1) = NaN;

total_notHTM = double(htm_sims_br(1:end-2,:) == 0);
total_notHTM(reset_ind1 == 1) = NaN;
total_notHTM(reset_ind2 == 1) = NaN;

HTM_HTM2 = (htm_sims_br(1:end-2,:) == 1).*(htm_sims_br(3:end,:) == 1);
HTM_HTM2(reset_ind1 == 1) = NaN;
HTM_HTM2(reset_ind2 == 1) = NaN;

HTM_notHTM2 = (htm_sims_br(1:end-2,:) == 1).*(htm_sims_br(3:end,:) == 0);
HTM_notHTM2(reset_ind1 == 1) = NaN;
HTM_notHTM2(reset_ind2 == 1) = NaN;


notHTM_HTM2 = (htm_sims_br(1:end-2,:) == 0).*(htm_sims_br(3:end,:) == 1);
notHTM_HTM2(reset_ind1 == 1) = NaN;
notHTM_HTM2(reset_ind2 == 1) = NaN;

notHTM_notHTM2 = (htm_sims_br(1:end-2,:) == 0).*(htm_sims_br(3:end,:) == 0);
notHTM_notHTM2(reset_ind1 == 1) = NaN;
notHTM_notHTM2(reset_ind2 == 1) = NaN;

HTM_transM_br = NaN(2,2);
HTM_transM_br(1,1) = nansum(HTM_HTM2(:))/nansum(total_HTM(:));
HTM_transM_br(1,2) = nansum(HTM_notHTM2(:))/nansum(total_HTM(:));
HTM_transM_br(2,1) = nansum(notHTM_HTM2(:))/nansum(total_notHTM(:));
HTM_transM_br(2,2) = nansum(notHTM_notHTM2(:))/nansum(total_notHTM(:));

total_HTM = double(htm_sims_re(1:end-2,:));
%total_HTM(reset_ind1 == 1) = NaN;
%total_HTM(reset_ind2 == 1) = NaN;

total_notHTM = double(htm_sims_re(1:end-2,:) == 0);
%total_notHTM(reset_ind1 == 1) = NaN;
%total_notHTM(reset_ind2 == 1) = NaN;

HTM_HTM2 = (htm_sims_re(1:end-2,:) == 1).*(htm_sims_re(3:end,:) == 1);
%HTM_HTM2(reset_ind1 == 1) = NaN;
%HTM_HTM2(reset_ind2 == 1) = NaN;

HTM_notHTM2 = (htm_sims_re(1:end-2,:) == 1).*(htm_sims_re(3:end,:) == 0);
%HTM_notHTM2(reset_ind1 == 1) = NaN;
%HTM_notHTM2(reset_ind2 == 1) = NaN;


notHTM_HTM2 = (htm_sims_re(1:end-2,:) == 0).*(htm_sims_re(3:end,:) == 1);
%notHTM_HTM2(reset_ind1 == 1) = NaN;
%notHTM_HTM2(reset_ind2 == 1) = NaN;

notHTM_notHTM2 = (htm_sims_re(1:end-2,:) == 0).*(htm_sims_re(3:end,:) == 0);
%notHTM_notHTM2(reset_ind1 == 1) = NaN;
%notHTM_notHTM2(reset_ind2 == 1) = NaN;

HTM_transM_re = NaN(2,2);
HTM_transM_re(1,1) = nansum(HTM_HTM2(:))/nansum(total_HTM(:));
HTM_transM_re(1,2) = nansum(HTM_notHTM2(:))/nansum(total_HTM(:));
HTM_transM_re(2,1) = nansum(notHTM_HTM2(:))/nansum(total_notHTM(:));
HTM_transM_re(2,2) = nansum(notHTM_notHTM2(:))/nansum(total_notHTM(:));

total_HTM = double(htm_sims_tr(1:end-2,:));
total_notHTM = double(htm_sims_tr(1:end-2,:) == 0);
HTM_HTM2 = (htm_sims_tr(1:end-2,:) == 1).*(htm_sims_tr(3:end,:) == 1);
HTM_notHTM2 = (htm_sims_tr(1:end-2,:) == 1).*(htm_sims_tr(3:end,:) == 0);



notHTM_HTM2 = (htm_sims_tr(1:end-2,:) == 0).*(htm_sims_tr(3:end,:) == 1);
notHTM_notHTM2 = (htm_sims_tr(1:end-2,:) == 0).*(htm_sims_tr(3:end,:) == 0);


HTM_transM_tr = NaN(2,2);
HTM_transM_tr(1,1) = nansum(HTM_HTM2(:))/nansum(total_HTM(:));
HTM_transM_tr(1,2) = nansum(HTM_notHTM2(:))/nansum(total_HTM(:));
HTM_transM_tr(2,1) = nansum(notHTM_HTM2(:))/nansum(total_notHTM(:));
HTM_transM_tr(2,2) = nansum(notHTM_notHTM2(:))/nansum(total_notHTM(:));


%%%% 4 period jump
reset_ind1 = reset_shocks( sample_half+1:end-3,:) < deltax;
reset_ind2 = reset_shocks( sample_half+2:end-2,:) < deltax;
reset_ind3 = reset_shocks( sample_half+3:end-1,:) < deltax;
reset_ind4 = reset_shocks( sample_half+4:end,:) < deltax;


total_HTM4 = double(htm_sims_br(1:end-4,:));
total_HTM4(reset_ind1 == 1) = NaN;
total_HTM4(reset_ind2 == 1) = NaN;
total_HTM4(reset_ind3 == 1) = NaN;
total_HTM4(reset_ind4 == 1) = NaN;

total_notHTM4 = double(htm_sims_br(1:end-4,:) == 0);
total_notHTM4(reset_ind1 == 1) = NaN;
total_notHTM4(reset_ind2 == 1) = NaN;
total_notHTM4(reset_ind3 == 1) = NaN;
total_notHTM4(reset_ind4 == 1) = NaN;

HTM_HTM4 = (htm_sims_br(1:end-4,:) == 1).*(htm_sims_br(5:end,:) == 1);
HTM_HTM4(reset_ind1 == 1) = NaN;
HTM_HTM4(reset_ind2 == 1) = NaN;
HTM_HTM4(reset_ind3 == 1) = NaN;
HTM_HTM4(reset_ind4 == 1) = NaN;

HTM_notHTM4 = (htm_sims_br(1:end-4,:) == 1).*(htm_sims_br(5:end,:) == 0);
HTM_notHTM4(reset_ind1 == 1) = NaN;
HTM_notHTM4(reset_ind2 == 1) = NaN;
HTM_notHTM4(reset_ind3 == 1) = NaN;
HTM_notHTM4(reset_ind4 == 1) = NaN;

notHTM_HTM4 = (htm_sims_br(1:end-4,:) == 0).*(htm_sims_br(5:end,:) == 1);
notHTM_HTM4(reset_ind1 == 1) = NaN;
notHTM_HTM4(reset_ind2 == 1) = NaN;
notHTM_HTM4(reset_ind3 == 1) = NaN;
notHTM_HTM4(reset_ind4 == 1) = NaN;

notHTM_notHTM4 = (htm_sims_br(1:end-4,:) == 0).*(htm_sims_br(5:end,:) == 0);
notHTM_notHTM4(reset_ind1 == 1) = NaN;
notHTM_notHTM4(reset_ind2 == 1) = NaN;
notHTM_notHTM4(reset_ind3 == 1) = NaN;
notHTM_notHTM4(reset_ind4 == 1) = NaN;

HTM_transM_br4 = NaN(2,2);
HTM_transM_br4(1,1) = nansum(HTM_HTM4(:))/nansum(total_HTM4(:));
HTM_transM_br4(1,2) = nansum(HTM_notHTM4(:))/nansum(total_HTM4(:));
HTM_transM_br4(2,1) = nansum(notHTM_HTM4(:))/nansum(total_notHTM4(:));
HTM_transM_br4(2,2) = nansum(notHTM_notHTM4(:))/nansum(total_notHTM4(:));


total_HTM4 = double(htm_sims_re(1:end-4,:));
%total_HTM4(reset_ind1 == 1) = NaN;
%total_HTM4(reset_ind2 == 1) = NaN;
%total_HTM4(reset_ind3 == 1) = NaN;
%total_HTM4(reset_ind4 == 1) = NaN;

total_notHTM4 = double(htm_sims_re(1:end-4,:) == 0);
%total_notHTM4(reset_ind1 == 1) = NaN;
%total_notHTM4(reset_ind2 == 1) = NaN;
%total_notHTM4(reset_ind3 == 1) = NaN;
%total_notHTM4(reset_ind4 == 1) = NaN;

HTM_HTM4 = (htm_sims_re(1:end-4,:) == 1).*(htm_sims_re(5:end,:) == 1);
%HTM_HTM4(reset_ind1 == 1) = NaN;
%HTM_HTM4(reset_ind2 == 1) = NaN;
%HTM_HTM4(reset_ind3 == 1) = NaN;
%HTM_HTM4(reset_ind4 == 1) = NaN;

HTM_notHTM4 = (htm_sims_re(1:end-4,:) == 1).*(htm_sims_re(5:end,:) == 0);
%HTM_notHTM4(reset_ind1 == 1) = NaN;
%HTM_notHTM4(reset_ind2 == 1) = NaN;
%HTM_notHTM4(reset_ind3 == 1) = NaN;
%HTM_notHTM4(reset_ind4 == 1) = NaN;

notHTM_HTM4 = (htm_sims_re(1:end-4,:) == 0).*(htm_sims_re(5:end,:) == 1);
%notHTM_HTM4(reset_ind1 == 1) = NaN;
%notHTM_HTM4(reset_ind2 == 1) = NaN;
%notHTM_HTM4(reset_ind3 == 1) = NaN;
%notHTM_HTM4(reset_ind4 == 1) = NaN;

notHTM_notHTM4 = (htm_sims_re(1:end-4,:) == 0).*(htm_sims_re(5:end,:) == 0);
%notHTM_notHTM4(reset_ind1 == 1) = NaN;
%notHTM_notHTM4(reset_ind2 == 1) = NaN;
%notHTM_notHTM4(reset_ind3 == 1) = NaN;
%notHTM_notHTM4(reset_ind4 == 1) = NaN;

HTM_transM_re4 = NaN(2,2);
HTM_transM_re4(1,1) = nansum(HTM_HTM4(:))/nansum(total_HTM4(:));
HTM_transM_re4(1,2) = nansum(HTM_notHTM4(:))/nansum(total_HTM4(:));
HTM_transM_re4(2,1) = nansum(notHTM_HTM4(:))/nansum(total_notHTM4(:));
HTM_transM_re4(2,2) = nansum(notHTM_notHTM4(:))/nansum(total_notHTM4(:));

total_HTM4 = double(htm_sims_tr(1:end-4,:));
total_notHTM4 = double(htm_sims_tr(1:end-4,:) == 0);
HTM_HTM4 = (htm_sims_tr(1:end-4,:) == 1).*(htm_sims_tr(5:end,:) == 1);
HTM_notHTM4 = (htm_sims_tr(1:end-4,:) == 1).*(htm_sims_tr(5:end,:) == 0);
notHTM_HTM4 = (htm_sims_tr(1:end-4,:) == 0).*(htm_sims_tr(5:end,:) == 1);
notHTM_notHTM4 = (htm_sims_tr(1:end-4,:) == 0).*(htm_sims_tr(5:end,:) == 0);

HTM_transM_tr4 = NaN(2,2);
HTM_transM_tr4(1,1) = nansum(HTM_HTM4(:))/nansum(total_HTM4(:));
HTM_transM_tr4(1,2) = nansum(HTM_notHTM4(:))/nansum(total_HTM4(:));
HTM_transM_tr4(2,1) = nansum(notHTM_HTM4(:))/nansum(total_notHTM4(:));
HTM_transM_tr4(2,2) = nansum(notHTM_notHTM4(:))/nansum(total_notHTM4(:));
%%% MPCs


mean_mpc_br_sims = mean(mpc_br,2);
mean_mpc_re_sims = mean(mpc_re,2);
mean_mpc_br_sims2 = mean(mpc_br2,2);
mean_mpc_re_sims2 = mean(mpc_re2,2);
mean_mpc_br_sims3 = mean(mpc_br3,2);
mean_mpc_re_sims3 = mean(mpc_re3,2);
mean_mpc_br_sims4 = mean(mpc_br4,2);
mean_mpc_re_sims4 = mean(mpc_re4,2);

median_mpc_br_sims = median(mpc_br,2);
median_mpc_re_sims = median(mpc_re,2);
median_mpc_br_sims2 = median(mpc_br2,2);
median_mpc_re_sims2 = median(mpc_re2,2);
median_mpc_br_sims3 = median(mpc_br3,2);
median_mpc_re_sims3 = median(mpc_re3,2);
median_mpc_br_sims4 = median(mpc_br4,2);
median_mpc_re_sims4 = median(mpc_re4,2);


mean_mpc_tr_sims = mean(mpc_tr,2);
mean_mpc_tr_sims2 = mean(mpc_tr2,2);
mean_mpc_tr_sims3 = mean(mpc_tr3,2);
mean_mpc_tr_sims4 = mean(mpc_tr4,2);

median_mpc_tr_sims = median(mpc_tr,2);
median_mpc_tr_sims2 = median(mpc_tr2,2);
median_mpc_tr_sims3 = median(mpc_tr3,2);
median_mpc_tr_sims4 = median(mpc_tr4,2);

std_mpc_br_sims = std(mpc_br,1,2);
std_mpc_re_sims = std(mpc_re,1,2);
std_mpc_tr_sims = std(mpc_tr,1,2);


prct5_mpc_br_sims = prctile(mpc_br,5,2);
prct5_mpc_re_sims = prctile(mpc_re,5,2);
prct10_mpc_br_sims = prctile(mpc_br,10,2);
prct10_mpc_re_sims = prctile(mpc_re,10,2);
prct90_mpc_br_sims = prctile(mpc_br,90,2);
prct90_mpc_re_sims = prctile(mpc_re,90,2);
prct95_mpc_br_sims = prctile(mpc_br,95,2);
prct95_mpc_re_sims = prctile(mpc_re,95,2);

prct5_mpc_tr_sims = prctile(mpc_tr,5,2);
prct10_mpc_tr_sims = prctile(mpc_tr,10,2);
prct90_mpc_tr_sims = prctile(mpc_tr,90,2);
prct95_mpc_tr_sims = prctile(mpc_tr,95,2);

mean_mpc_br = mean( mean_mpc_br_sims(sample_half:end));
mean_mpc_re = mean( mean_mpc_re_sims(sample_half:end));
mean_mpc_br2 = mean( mean_mpc_br_sims2(sample_half:end));
mean_mpc_re2 = mean( mean_mpc_re_sims2(sample_half:end));
mean_mpc_br3 = mean( mean_mpc_br_sims3(sample_half:end));
mean_mpc_re3 = mean( mean_mpc_re_sims3(sample_half:end));
mean_mpc_br4 = mean( mean_mpc_br_sims4(sample_half:end));
mean_mpc_re4 = mean( mean_mpc_re_sims4(sample_half:end));

mean_mpc_tr = mean( mean_mpc_tr_sims(sample_half:end));
mean_mpc_tr2 = mean( mean_mpc_tr_sims2(sample_half:end));
mean_mpc_tr3 = mean( mean_mpc_tr_sims3(sample_half:end));
mean_mpc_tr4 = mean( mean_mpc_tr_sims4(sample_half:end));

median_mpc_br = mean( median_mpc_br_sims(sample_half:end));
median_mpc_re = mean( median_mpc_re_sims(sample_half:end));
median_mpc_br2 = mean( median_mpc_br_sims2(sample_half:end));
median_mpc_re2 = mean( median_mpc_re_sims2(sample_half:end));
median_mpc_br3 = mean( median_mpc_br_sims3(sample_half:end));
median_mpc_re3 = mean( median_mpc_re_sims3(sample_half:end));
median_mpc_br4 = mean( median_mpc_br_sims4(sample_half:end));
median_mpc_re4 = mean( median_mpc_re_sims4(sample_half:end));

median_mpc_tr = mean( median_mpc_tr_sims(sample_half:end));
median_mpc_tr2 = mean( median_mpc_tr_sims2(sample_half:end));
median_mpc_tr3 = mean( median_mpc_tr_sims3(sample_half:end));
median_mpc_tr4 = mean( median_mpc_tr_sims4(sample_half:end));

prct5_mpc_br=mean(prct5_mpc_br_sims(sample_half:end));
prct5_mpc_re=mean(prct5_mpc_re_sims(sample_half:end));
prct10_mpc_br=mean(prct10_mpc_br_sims(sample_half:end));
prct10_mpc_re=mean(prct10_mpc_re_sims(sample_half:end));
prct90_mpc_br=mean(prct90_mpc_br_sims(sample_half:end));
prct90_mpc_re=mean(prct90_mpc_re_sims(sample_half:end));
prct95_mpc_br=mean(prct95_mpc_br_sims(sample_half:end));
prct95_mpc_re=mean(prct95_mpc_re_sims(sample_half:end));

prct5_mpc_tr=mean(prct5_mpc_tr_sims(sample_half:end));
prct10_mpc_tr=mean(prct10_mpc_tr_sims(sample_half:end));
prct90_mpc_tr=mean(prct90_mpc_tr_sims(sample_half:end));
prct95_mpc_tr=mean(prct95_mpc_tr_sims(sample_half:end));


std_mpc_br = mean( std_mpc_br_sims(sample_half:end));
std_mpc_re = mean( std_mpc_re_sims(sample_half:end));
std_mpc_tr = mean( std_mpc_tr_sims(sample_half:end));

mean_mpc_br_abovemed_sims = NaN( sim_periods -sample_half,1);
mean_mpc_re_abovemed_sims = NaN( sim_periods -sample_half,1);
mean_mpc_br_nonza_sims = NaN( sim_periods -sample_half,1);
mean_mpc_re_nonza_sims = NaN( sim_periods -sample_half,1);

mpc_br_aquint_sims = NaN(sim_periods - sample_half,5);
mpc_re_aquint_sims = NaN(sim_periods - sample_half,5);

mpc_br_top1_sims  = NaN(sim_periods - sample_half,1);
mpc_br_top10_sims = NaN(sim_periods - sample_half,1);

mpc_re_top1_sims  = NaN(sim_periods - sample_half,1);
mpc_re_top10_sims = NaN(sim_periods - sample_half,1);

mean_mpc_tr_abovemed_sims = NaN( sim_periods -sample_half,1);
mean_mpc_tr_nonza_sims = NaN( sim_periods -sample_half,1);

mpc_tr_aquint_sims = NaN(sim_periods - sample_half,5);

mpc_tr_top1_sims  = NaN(sim_periods - sample_half,1);
mpc_tr_top10_sims = NaN(sim_periods - sample_half,1);

for i = 1:sim_periods - sample_half
    
    mean_mpc_br_abovemed_sims(i) = mean(mpc_br(i + sample_half, a_sims_br(i + sample_half,:) > median(a_sims_br(i + sample_half,:))));
    mean_mpc_re_abovemed_sims(i) = mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) > median(a_sims_re(i + sample_half,:))));
    mean_mpc_tr_abovemed_sims(i) = mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) > median(a_sims_tr(i + sample_half,:))));
    
    mean_mpc_br_nonza_sims(i) = mean(mpc_br(i + sample_half, a_sims_br(i + sample_half,:) > 0));
    mean_mpc_re_nonza_sims(i) = mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) > 0));
    mean_mpc_tr_nonza_sims(i) = mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) > 0));
    
    quint_temp = prctile( a_sims_br(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);
    mpc_br_aquint_sims(i,1) =  mean(mpc_br3(i + sample_half, a_sims_br(i + sample_half,:) <= quint_temp(1)));
    mpc_br_aquint_sims(i,2) =  mean(mpc_br3(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)));
    mpc_br_aquint_sims(i,3) =  mean(mpc_br3(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)));
    mpc_br_aquint_sims(i,4) =  mean(mpc_br3(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)));
    mpc_br_aquint_sims(i,5) =  mean(mpc_br3(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(4)));
    
    quint_temp = prctile( a_sims_re(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);
    mpc_re_aquint_sims(i,1) =  mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) <= quint_temp(1)));
    mpc_re_aquint_sims(i,2) =  mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) > quint_temp(1) &  a_sims_re(i + sample_half,:) <= quint_temp(2)));
    mpc_re_aquint_sims(i,3) =  mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) > quint_temp(2) &  a_sims_re(i + sample_half,:) <= quint_temp(3)));
    mpc_re_aquint_sims(i,4) =  mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) > quint_temp(3) &  a_sims_re(i + sample_half,:) <= quint_temp(4)));
    mpc_re_aquint_sims(i,5) =  mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) > quint_temp(4)));
    
    quint_temp = prctile( a_sims_tr(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);
    mpc_tr_aquint_sims(i,1) =  mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) <= quint_temp(1)));
    mpc_tr_aquint_sims(i,2) =  mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) > quint_temp(1) &  a_sims_tr(i + sample_half,:) <= quint_temp(2)));
    mpc_tr_aquint_sims(i,3) =  mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) > quint_temp(2) &  a_sims_tr(i + sample_half,:) <= quint_temp(3)));
    mpc_tr_aquint_sims(i,4) =  mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) > quint_temp(3) &  a_sims_tr(i + sample_half,:) <= quint_temp(4)));
    mpc_tr_aquint_sims(i,5) =  mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) > quint_temp(4)));
    
    prct_temp = prctile(a_sims_br(i + sample_half,:), [90,99]);
    
    mpc_br_top1_sims(i)  =  mean(mpc_br(i + sample_half, a_sims_br(i + sample_half,:) >= prct_temp(2)));
    mpc_br_top10_sims(i) =  mean(mpc_br(i + sample_half, a_sims_br(i + sample_half,:) >= prct_temp(1)));
    
    prct_temp = prctile(a_sims_re(i + sample_half,:), [90,99]);
    mpc_re_top1_sims(i)  =  mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) >= prct_temp(2)));
    mpc_re_top10_sims(i) =  mean(mpc_re(i + sample_half, a_sims_re(i + sample_half,:) >= prct_temp(1)));
    
    prct_temp = prctile(a_sims_tr(i + sample_half,:), [90,99]);
    mpc_tr_top1_sims(i)  =  mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) >= prct_temp(2)));
    mpc_tr_top10_sims(i) =  mean(mpc_tr(i + sample_half, a_sims_tr(i + sample_half,:) >= prct_temp(1)));
    
end

mean_mpc_br_abovemed = mean(mean_mpc_br_abovemed_sims);
mean_mpc_re_abovemed = mean(mean_mpc_re_abovemed_sims);
mean_mpc_br_nonza = mean(mean_mpc_br_nonza_sims);
mean_mpc_re_nonza = mean(mean_mpc_re_nonza_sims);

mean_mpc_tr_abovemed = mean(mean_mpc_tr_abovemed_sims);
mean_mpc_tr_nonza = mean(mean_mpc_tr_nonza_sims);

figure
histogram(mpc_br(sample_half:end,:))

mpc_htm_br = mpc_br(sample_half:end,:);
mpc_htm_br = mpc_htm_br(htm_sims_br == 1);

mpc_nonhtm_br = mpc_br(sample_half:end,:);
mpc_nonhtm_br = mpc_nonhtm_br(htm_sims_br == 0);
mpc_nonhtm_br2 = mpc_br2(sample_half:end,:);
mpc_nonhtm_br2 = mpc_nonhtm_br2(htm_sims_br == 0);
mpc_nonhtm_br3 = mpc_br3(sample_half:end,:);
mpc_nonhtm_br3 = mpc_nonhtm_br3(htm_sims_br == 0);
mpc_nonhtm_br4 = mpc_br4(sample_half:end,:);
mpc_nonhtm_br4 = mpc_nonhtm_br4(htm_sims_br == 0);

mpc_htm_re = mpc_re(sample_half:end,:);
mpc_htm_re = mpc_htm_re(htm_sims_re == 1);

mpc_nonhtm_re = mpc_re(sample_half:end,:);
mpc_nonhtm_re = mpc_nonhtm_re(htm_sims_re == 0);
mpc_nonhtm_re2 = mpc_re2(sample_half:end,:);
mpc_nonhtm_re2 = mpc_nonhtm_re2(htm_sims_re == 0);
mpc_nonhtm_re3 = mpc_re3(sample_half:end,:);
mpc_nonhtm_re3 = mpc_nonhtm_re3(htm_sims_re == 0);
mpc_nonhtm_re4 = mpc_re4(sample_half:end,:);
mpc_nonhtm_re4 = mpc_nonhtm_re4(htm_sims_re == 0);

mpc_htm_tr = mpc_tr(sample_half:end,:);
mpc_htm_tr = mpc_htm_tr(htm_sims_tr == 1);

mpc_nonhtm_tr = mpc_tr(sample_half:end,:);
mpc_nonhtm_tr = mpc_nonhtm_tr(htm_sims_tr == 0);
mpc_nonhtm_tr2 = mpc_tr2(sample_half:end,:);
mpc_nonhtm_tr2 = mpc_nonhtm_tr2(htm_sims_tr == 0);
mpc_nonhtm_tr3 = mpc_tr3(sample_half:end,:);
mpc_nonhtm_tr3 = mpc_nonhtm_tr3(htm_sims_tr == 0);
mpc_nonhtm_tr4 = mpc_tr4(sample_half:end,:);
mpc_nonhtm_tr4 = mpc_nonhtm_tr4(htm_sims_tr == 0);

%%%% Simple MPC calculations
ya_br_sims = a_sims_br*(1+rt_br) + w_br*yi;
diff_ya_br_sims = diff(ya_br_sims);
diff_ya_br_sims(reset_shocks(2:end,:) < deltax) = NaN;
diff_chat_br_sims = diff(chat_sims_br);

simple_mpc_br_sims  = NaN( size(a_sims_br));
diff_chat_br_sims(age_sims_br(2:end,:) == 1) = NaN; 
simple_mpc_br_sims(2:end,:) = diff_chat_br_sims./diff_ya_br_sims;

median_simple_mpc_br = mean(nanmedian( simple_mpc_br_sims(sample_half:end,:),2));


%%% Reasoning statistics

mean_alpha = mean(mean(alpha_star_sims(sample_half+1:end,:)));
std_alpha  = mean(std(alpha_star_sims(sample_half+1:end,:)));
median_alpha  = mean(median(alpha_star_sims(sample_half+1:end,:)));

alpha_dist = NaN(sim_periods - sample_half, nsim);
alphazero_frac = NaN(sim_periods - sample_half, 1);
avg_alpha_alphaquint = NaN(sim_periods - sample_half, 5);
avg_alpha_aquint = NaN(sim_periods - sample_half, 5);
alphanz_frac_aquint = NaN(sim_periods - sample_half,5);

for i = 1:sim_periods - sample_half
    
    alpha_sort_temp =  sort(alpha_star_sims(sample_half+1,:));
    alpha_dist(i,:) = alpha_sort_temp;
    alphazero_frac(i) = sum(alpha_sort_temp == 0)/nsim;
    
    %quint_temp = prctile( alpha_star_sims(sample_half+1,:), 100*[0.2,0.4,0.6,0.8]);
    avg_alpha_alphaquint(i,1) = mean( alpha_sort_temp(1:nsim/5));
    avg_alpha_alphaquint(i,2) = mean( alpha_sort_temp(nsim/5+1:2*nsim/5));
    avg_alpha_alphaquint(i,3) = mean( alpha_sort_temp(2*nsim/5+1:3*nsim/5));
    avg_alpha_alphaquint(i,4) = mean( alpha_sort_temp(3*nsim/5+1:4*nsim/5));
    avg_alpha_alphaquint(i,5) = mean( alpha_sort_temp(4*nsim/5+1:end));
    
    quint_temp = prctile( a_sims_br(i + sample_half,:), 100*[0.2, 0.4, 0.6, 0.8]);
    avg_alpha_aquint(i,1) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) <= quint_temp(1)));
    avg_alpha_aquint(i,2) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)));
    avg_alpha_aquint(i,3) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)));
    avg_alpha_aquint(i,4) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)));
    avg_alpha_aquint(i,5) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(4)));
    
    alphanz_frac_aquint(i,1) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) <= quint_temp(1)) > 0);
    alphanz_frac_aquint(i,2) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(1) &  a_sims_br(i + sample_half,:) <= quint_temp(2)) > 0);
    alphanz_frac_aquint(i,3) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(2) &  a_sims_br(i + sample_half,:) <= quint_temp(3)) > 0);
    alphanz_frac_aquint(i,4) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(3) &  a_sims_br(i + sample_half,:) <= quint_temp(4)) > 0);
    alphanz_frac_aquint(i,5) =  mean(alpha_star_sims(i + sample_half, a_sims_br(i + sample_half,:) > quint_temp(4)) > 0);
    
    
end

max_alpha = max( max(alpha_star_sims(sample_half+1:end,:)));

figure

histogram( mean(alpha_dist),'Normalization','probability')

%%% Aggregate effects of reasoning erros

cbar_br = mean(chat_sims_br,2);
cbar_br_cstar = mean(cstar_sims_br,2);
cbar_re = mean(cstar_sims_re,2);

cbar_br_anal = NaN( sim_periods, nsim);

median_alpha_sims  = median(alpha_star_sims,2);

for i = 1:nsim
    cbar_br_anal(:,i) =  min( [cstar_sims_br(:,i) +   (1- alpha_star_sims(:,i)).*(chat_prime_sims(:,i) - cstar_sims_br(:,i)), a_sims_br(:,i)*(1 + rt_br) + w_br*yi(:,i) - b] ,[],2);
    %cbar_br_anal(:,i) =  min( [cstar_sims_br(:,i) +   (1- median_alpha_sims).*(chat_prime_sims(:,i) - cstar_sims_br(:,i)), a_sims_br(:,i)*(1 + rt_br) + w_br*yi(:,i) - b] ,[],2);
    
end



agg_error_aquint = NaN(sample_half,5);

for i = 1:sim_periods - sample_half
    [~,sort_ind] = sort( a_sims_br(i+sample_half,:));
    
    agg_error_aquint(i,1) = mean( cbar_br_anal(i + sample_half,sort_ind(1: round(nsim/5))) - cstar_sims_br( i + sample_half, sort_ind(1:round(nsim/5))),2);
    agg_error_aquint(i,2) = mean( cbar_br_anal(i + sample_half,sort_ind(round(nsim/5) + 1:2*round(nsim/5))) - cstar_sims_br( i + sample_half,sort_ind(round(nsim/5)+1:2*round(nsim/5))),2);
    agg_error_aquint(i,3) = mean( cbar_br_anal(i + sample_half,sort_ind(2*round(nsim/5) + 1:3*round(nsim/5))) - cstar_sims_br( i + sample_half,sort_ind(2*round(nsim/5)+1:3*round(nsim/5))),2);
    agg_error_aquint(i,4) = mean( cbar_br_anal(i + sample_half,sort_ind(3*round(nsim/5) + 1:4*round(nsim/5))) - cstar_sims_br( i + sample_half,sort_ind(3*round(nsim/5)+1:4*round(nsim/5))),2);
    agg_error_aquint(i,5) = mean( cbar_br_anal(i + sample_half,sort_ind(4*round(nsim/5) + 1:5*round(nsim/5))) - cstar_sims_br( i + sample_half,sort_ind(4*round(nsim/5)+1:5*round(nsim/5))),2);
    
end

cbar_br_anal = mean(cbar_br_anal,2);
agg_error    = cbar_br_anal - cbar_br_cstar;

plot( [cbar_br_anal, cbar_br, cbar_br_cstar])
temp = mean( chat_prime_sims + alpha_star_sims.*( eta_sims  - chat_prime_sims), 2);

%%% power law stuff

[a, xmin, L] = plfit(wealth_dist_br,'limit',7);


%[a_br, xmin_br, L_br] = plfit(wealth_dist_br,'nosmall');
%[a_re, xmin_re, L_re] = plfit(wealth_dist_re,'nosmall');

%[a_re, xmin_re, L_re] = plfit(a_sims_re(700,:),'nosmall')

%yi_temp = w_re*yi(:,1:10);
%[a_yi, xmin_yi, L_yi] = plfit(yi_temp(:),'nosmall');

%plplot( wealth_dist_br(wealth_dist_br > 0), xmin_br, a_br);
%[pval_br, gof_br] = plpva( wealth_dist_br, 13.98, 'nosmall');

%rw_pol = (rt_br/(1+rt_br))*stateGrid' + 1/(1+rt_br)*w_br;

rw_pol_c = (rt_br/(1+rt_br))*((1+rt_br)*a_sims_br + w_br*yi) + 1/(1+rt_br)*w_br;

chat_below_rw = NaN(maxiter_distr,1);
chat_above_rw = NaN(maxiter_distr,1);

for i = 1:maxiter_distr
    chat_below_rw(i) = mean(chat_sims_br(i, a_sims_br(i,:) > median(a_sims_br(i,:))) - rw_pol_c(i, a_sims_br(i,:) > median(a_sims_br(i,:))) < 0,2);
    chat_above_rw(i) = mean(chat_sims_br(i, a_sims_br(i,:) > median(a_sims_br(i,:))) - rw_pol_c(i, a_sims_br(i,:) > median(a_sims_br(i,:))) > 0, 2);
end

%%%
%%
disp(' ');
disp(' --------------------------------')
disp([' Wealth Distribution moments '])
disp(' --------------------------------')
disp( '                              Data    BR     RE    Trmbl ');
disp([' K                           :  ' , sprintf('%1.2f   ', [NaN, K_br, K_re, K_tr] )  ]);
disp([' Mean                        :  ' , sprintf('%1.2f   ', [NaN, mean([ wealth_dist_br, wealth_dist_re, wealth_dist_tr])] )  ]);
disp([' Median                      :  ' , sprintf('%1.2f   ', [NaN, median([ wealth_dist_br, wealth_dist_re, wealth_dist_tr])] )  ]);
disp([' Std                         :  ' , sprintf('%1.2f   ', [NaN, std([ wealth_dist_br, wealth_dist_re, wealth_dist_tr ])]  )]);
disp([' Kurt                        :  ' , sprintf('%1.2f   ', [NaN, kurtosis([ wealth_dist_br, wealth_dist_re, wealth_dist_tr ])]  )]);
disp([' Gini                        :  ' , sprintf('%1.2f   ', [ 0.78, gini_br_mean, gini_re_mean, gini_tr_mean ]  )]);
disp([' Q1 asset share              :  ' , sprintf('%1.2f   ', 100*[ -.002, wealth_sh_quint_br(1), wealth_sh_quint_re(1), wealth_sh_quint_tr(1)]  )  ]);
disp([' Q2 asset share              :  ' , sprintf('%1.2f   ', 100*[ 0.012, wealth_sh_quint_br(2), wealth_sh_quint_re(2), wealth_sh_quint_tr(2)]  )  ]);
disp([' Q3 asset share              :  ' , sprintf('%1.2f   ', 100*[ 0.044, wealth_sh_quint_br(3), wealth_sh_quint_re(3), wealth_sh_quint_tr(3)]  )  ]);
disp([' Q4 asset share              :  ' , sprintf('%1.2f   ', 100*[ 0.13, wealth_sh_quint_br(4), wealth_sh_quint_re(4),  wealth_sh_quint_tr(4)]  )  ]);
disp([' Q5 asset share              :  ' , sprintf('%1.2f   ', 100*[ 0.827, wealth_sh_quint_br(5), wealth_sh_quint_re(5), wealth_sh_quint_tr(5)]  )  ]);
disp([' >99%   share                :  ' , sprintf('%1.2f   ', 100*[ 0.309, wealthsh_top1_br, wealthsh_top1_re, wealthsh_top1_tr ]  )  ]);
disp([' 95-99% share                :  ' , sprintf('%1.2f   ', 100*[ .228, wealthsh_top5_br - wealthsh_top1_br, wealthsh_top5_re - wealthsh_top1_re, wealthsh_top5_tr - wealthsh_top1_tr  ]     )  ]);
disp([' 90-95% share                :  ' , sprintf('%1.2f   ', 100*[ 0.137, wealthsh_top10_br - wealthsh_top5_br, wealthsh_top10_re - wealthsh_top5_re, wealthsh_top10_tr - wealthsh_top5_tr  ]   )  ]);
disp([' hand-to-mouth agents (%)    :  ' , sprintf('%1.2f   ', 100*[ NaN, handtomouthsh_br, handtomouthsh_re, handtomouthsh_tr  ]   )  ]);
disp([' negative assets      (%)    :  ' , sprintf('%1.2f   ', 100*[ NaN, nega_br, nega_re  ]   )  ]);
disp([' Shorrcks Ind (5yr)          :  ' , sprintf('%1.2f   ', [ 0.661, s_br_5yr, s_re_5yr ]   )  ]);
disp([' HTM-HTM (2yrs)              :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br(1,1), HTM_transM_re(1,1), HTM_transM_tr(1,1)]   )  ]);
disp([' HTM-notHTM (2yrs)           :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br(1,2), HTM_transM_re(1,2), HTM_transM_tr(1,2)]   )  ]);
disp([' notHTM-HTM (2yrs)           :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br(2,1), HTM_transM_re(2,1), HTM_transM_tr(2,1)]   )  ]);
disp([' notHTM-notHTM (2yrs)        :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br(2,2), HTM_transM_re(2,2), HTM_transM_tr(2,2)]   )  ]);
disp([' HTM-HTM (4yrs)              :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br4(1,1), HTM_transM_re4(1,1), HTM_transM_tr4(1,1)]   )  ]);
disp([' HTM-notHTM (4yrs)           :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br4(1,2), HTM_transM_re4(1,2), HTM_transM_tr4(1,2)]   )  ]);
disp([' notHTM-HTM (4yrs)           :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br4(2,1), HTM_transM_re4(2,1), HTM_transM_tr4(2,1)]   )  ]);
disp([' notHTM-notHTM (4yrs)        :  ' , sprintf('%1.2f   ', [ NaN, HTM_transM_br4(2,2), HTM_transM_re4(2,2), HTM_transM_tr4(2,2)]   )  ]);

disp(' ');
disp(' --------------------------------')
disp([' Marginal Propensity to Consume '])
disp(' --------------------------------')
disp( '                           Data    BR     RE    Trmbl ');
disp([' Avg MPC                         :  ' , sprintf('%1.4f   ', [NaN,  mean_mpc_br, mean_mpc_re, mean_mpc_tr] )  ]);
disp([' Median MPC                      :  ' , sprintf('%1.4f   ', [NaN,  median_mpc_br, median_mpc_re, median_mpc_tr] )  ]);
disp([' Avg MPC (-shock)                :  ' , sprintf('%1.4f   ', [NaN,  mean_mpc_br2, mean_mpc_re2, mean_mpc_tr2] )  ]);
disp([' Median MPC (-shock)             :  ' , sprintf('%1.4f   ', [NaN,  median_mpc_br2, median_mpc_re2, median_mpc_re2] )  ]);
disp([' Avg MPC  (big shock)            :  ' , sprintf('%1.4f   ', [NaN,  mean_mpc_br3, mean_mpc_re3, mean_mpc_tr3] )  ]);
disp([' Median MPC (big shock)          :  ' , sprintf('%1.4f   ', [NaN,  median_mpc_br3, median_mpc_re3, median_mpc_tr3] )  ]);
disp([' Avg MPC (-big shock)            :  ' , sprintf('%1.4f   ', [NaN,  mean_mpc_br4, mean_mpc_re4, mean_mpc_tr4] )  ]);
disp([' Median MPC (-bigshock)          :  ' , sprintf('%1.4f   ', [NaN,  median_mpc_br4, median_mpc_re4, median_mpc_tr4] )  ]);
disp([' Standard dev MPC                :  ' , sprintf('%1.4f   ', [NaN,  std_mpc_br, std_mpc_re, std_mpc_tr] )  ]);
disp([' Bottom 5% MPC                   :  ' , sprintf('%1.4f   ', [NaN,  prct5_mpc_br, prct5_mpc_re ] ) ] );
disp([' Top 95% MPC                     :  ' , sprintf('%1.4f   ', [NaN,  prct95_mpc_br, prct95_mpc_re ] )  ]);
disp([' Bottom 10% MPC                  :  ' , sprintf('%1.4f   ', [NaN,  prct10_mpc_br, prct10_mpc_re ] ) ] );
disp([' Top 90% MPC                     :  ' , sprintf('%1.4f   ', [NaN,  prct90_mpc_br, prct90_mpc_re ] )  ]);
disp([' Mean MPC | Q1(a)                :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_br_aquint_sims(:,1)), mean(mpc_re_aquint_sims(:,1)), nanmean(mpc_tr_aquint_sims(:,1))] )  ]);
disp([' Mean MPC | Q2(a)                :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_br_aquint_sims(:,2)), mean(mpc_re_aquint_sims(:,2)), nanmean(mpc_tr_aquint_sims(:,2))] )  ]);
disp([' Mean MPC | Q3(a)                :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_br_aquint_sims(:,3)), mean(mpc_re_aquint_sims(:,3)), nanmean(mpc_tr_aquint_sims(:,3))] )  ]);
disp([' Mean MPC | Q4(a)                :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_br_aquint_sims(:,4)), mean(mpc_re_aquint_sims(:,4)), nanmean(mpc_tr_aquint_sims(:,4))] )  ]);
disp([' Mean MPC | Q5(a)                :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_br_aquint_sims(:,5)), mean(mpc_re_aquint_sims(:,5)), nanmean(mpc_tr_aquint_sims(:,5))] )  ]);
disp([' Mean MPC | top10%               :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_br_top10_sims(:)), mean(mpc_re_top10_sims(:)), mean(mpc_tr_top10_sims(:))] )  ]);
disp([' Mean MPC | top1%                :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_br_top1_sims(:)), mean(mpc_re_top1_sims(:)), mean(mpc_tr_top1_sims(:))] )  ]);
disp([' Avg MPC | a > median            :  ' , sprintf('%1.4f   ', [NaN,  mean_mpc_br_abovemed, mean_mpc_re_abovemed, mean_mpc_tr_abovemed])  ]);
disp([' Avg MPC | a > 0                 :  ' , sprintf('%1.4f   ', [NaN,  mean_mpc_br_nonza, mean_mpc_re_nonza] )  ]);
disp([' Avg regression MPC              :  ' , sprintf('%1.4f   ', [NaN,  nanmean(mpc_reg_br), nanmean(mpc_reg_re)] )  ]);
disp([' MPC | HTM                       :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_htm_br), mean(mpc_htm_re), mean(mpc_htm_tr)] )  ]);
disp([' MPC | non-HTM                   :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_nonhtm_br), mean(mpc_nonhtm_re), mean(mpc_nonhtm_tr)] )  ]);
disp([' MPC | non-HTM (neg)             :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_nonhtm_br2), mean(mpc_nonhtm_re2), mean(mpc_nonhtm_tr2)] )  ]);
disp([' MPC | non-HTM (big y)           :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_nonhtm_br3), mean(mpc_nonhtm_re3), mean(mpc_nonhtm_tr3)] )  ]);
disp([' MPC | non-HTM (big -y)          :  ' , sprintf('%1.4f   ', [NaN,  mean(mpc_nonhtm_br4), mean(mpc_nonhtm_re4), mean(mpc_nonhtm_tr4)] )  ]);
disp([' APC | HTM vs non-HTM            :  ' , sprintf('%1.4f   ', [NaN,  apc_br(2), apc_re(2), apc_tr(2)] )  ]);
disp([' Dy  | HTM vs non-HTM            :  ' , sprintf('%1.4f   ', [NaN,  yhtm_reg_br(2), yhtm_reg_re(2), yhtm_reg_tr(2)] )  ]);
disp([' Dc  | HTM vs non-HTM            :  ' , sprintf('%1.4f   ', [NaN,  chtm_reg_br(2), chtm_reg_re(2), chtm_reg_tr(2)] )  ]);
disp([' Dy fe | HTM vs non-HTM          :  ' , sprintf('%1.4f   ', [NaN,  nanmean(yhtm_reg_indiv_br), nanmean(yhtm_reg_indiv_re), nanmean(yhtm_reg_indiv_tr)] )  ]);
disp([' Dc fe | HTM vs non-HTM          :  ' , sprintf('%1.4f   ', [NaN,  nanmedian(chtm_reg_indiv_br), nanmedian(chtm_reg_indiv_re), nanmedian(chtm_reg_indiv_tr)] )  ]);
disp([' Dy(HTM)| HTM vs non-HTM         :  ' , sprintf('%1.4f   ', [NaN,  yhtm_reg_br2(2), yhtm_reg_re2(2), yhtm_reg_tr2(2)] )  ]);
disp([' Dy(HTM frac)| HTM vs non-HTM    :  ' , sprintf('%1.4f   ', [NaN,  yhtm_reg_br2(3), yhtm_reg_re2(3), yhtm_reg_tr2(3)] )  ]);
disp([' Dc(HTM)| HTM vs non-HTM         :  ' , sprintf('%1.4f   ', [NaN,  chtm_reg_br2(2), chtm_reg_re2(2), chtm_reg_tr2(2)] )  ]);
disp([' Dc(HTM frac)| HTM vs non-HTM    :  ' , sprintf('%1.4f   ', [NaN,  chtm_reg_br2(3), chtm_reg_re2(3), chtm_reg_tr2(3)] )  ]);
disp([' APC(HTM) | HTM vs non-HTM       :  ' , sprintf('%1.4f   ', [NaN,  apc_br2(2), apc_re2(2), apc_tr2(2)] )  ]);
disp([' APC(HTM frac) | HTM vs non-HTM  :  ' , sprintf('%1.4f   ', [NaN,  apc_br2(3), apc_re2(3), apc_tr2(3)] )  ]);


disp(' ');

disp(' --------------------------------')
disp([' Signal-to-noise Ratio Statistics '])
disp(' --------------------------------')
disp( '                          Data    BR     RE    Trmbl ');
disp([' Avg alpha               :  ' , sprintf('%1.4f   ', [NaN,  mean_alpha, NaN] )  ]);
disp([' Median alpha            :  ' , sprintf('%1.4f   ', [NaN,  median_alpha, NaN] )  ]);
disp([' Std alpha               :  ' , sprintf('%1.4f   ', [NaN,  std_alpha, NaN] )  ]);
disp([' Max alpha               :  ' , sprintf('%1.4f   ', [NaN,  max_alpha, NaN] )  ]);
disp([' Fraction alpha == 0     :  ' , sprintf('%1.4f   ', [NaN,  mean(alphazero_frac), NaN] )  ]);
disp([' Avg alph | Q1(alpha)    :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_alphaquint(:,1)), NaN] )  ]);
disp([' Avg alph | Q2(alpha)    :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_alphaquint(:,2)), NaN] )  ]);
disp([' Avg alph | Q3(alpha)    :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_alphaquint(:,3)), NaN] )  ]);
disp([' Avg alph | Q4(alpha)    :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_alphaquint(:,4)), NaN] )  ]);
disp([' Avg alph | Q5(alpha)    :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_alphaquint(:,5)), NaN] )  ]);
disp([' Avg alph | Q1(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_aquint(:,1)), NaN] )  ]);
disp([' Avg alph | Q2(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_aquint(:,2)), NaN] )  ]);
disp([' Avg alph | Q3(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_aquint(:,3)), NaN] )  ]);
disp([' Avg alph | Q4(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_aquint(:,4)), NaN] )  ]);
disp([' Avg alph | Q5(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(avg_alpha_aquint(:,5)), NaN] )  ]);
disp([' alpha~=0 | Q1(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(alphanz_frac_aquint(:,1)), NaN] )  ]);
disp([' alpha~=0 | Q2(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(alphanz_frac_aquint(:,2)), NaN] )  ]);
disp([' alpha~=0 | Q3(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(alphanz_frac_aquint(:,3)), NaN] )  ]);
disp([' alpha~=0 | Q4(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(alphanz_frac_aquint(:,4)), NaN] )  ]);
disp([' alpha~=0 | Q5(assets)   :  ' , sprintf('%1.4f   ', [NaN,  mean(alphanz_frac_aquint(:,5)), NaN] )  ]);
%% Covid shock

htm_sims_infoshock    = a_sims_infoshock_br <= w_br.*yi_infoshock/6;


gini_infoshock = NaN(size(a_sims_infoshock_br,1),1);
mpc_nonhtm_infoshock = NaN(size(a_sims_infoshock_br,1), 1);
mpc_nonhtm_infoshock2 = NaN(size(a_sims_infoshock_br,1), 1);
wealthsh_top1_infoshock = NaN(size(a_sims_infoshock_br,1),1);
wealthsh_top10_infoshock = NaN(size(a_sims_infoshock_br,1),1);
wealthsh_bot20_infoshock = NaN(size(a_sims_infoshock_br,1),1);
mpc_q5_infoshock = NaN(size(a_sims_infoshock_br,1),1);
mpc_q5_infoshock2 = NaN(size(a_sims_infoshock_br,1),1);

for i = 1:size(a_sims_infoshock_br,1)
    
    gini_infoshock(i) = gini( ones(nsim,1), (a_sims_infoshock_br(i,:).*(a_sims_infoshock_br(i,:)>=0))');
    mpc_nonhtm_infoshock(i) = mean( mpc_br_infoshock(i, htm_sims_infoshock(i,:) == 0));
    mpc_nonhtm_infoshock2(i) = median( mpc_br_infoshock(i, htm_sims_infoshock(i,:) == 0));
    
    [sorted_a,sort_ind] = sort( a_sims_infoshock_br(i,:));
    wealthsh_top1_infoshock(i) = sum( a_sims_infoshock_br(i, sort_ind( end-nsim/100+1:end)))/sum( a_sims_infoshock_br(i,:));
    wealthsh_top10_infoshock(i) = sum( a_sims_infoshock_br(i, sort_ind( end-nsim/10+1:end)))/sum( a_sims_infoshock_br(i,:));
    wealthsh_bot20_infoshock(i) = sum( a_sims_infoshock_br(i, sort_ind( 1:nsim/5)))/sum( a_sims_infoshock_br(i,:));
    
    mpc_q5_infoshock(i)   = mean( mpc_br_infoshock(i, sort_ind(end-nsim*0.2+1:end)));
    mpc_q5_infoshock2(i)   = median( mpc_br_infoshock(i, sort_ind(end-nsim*0.2+1:end)));
    
end


mean_htm_infoshock = mean(htm_sims_infoshock,2);
mean_mpc_infoshock_sims = mean(mpc_br_infoshock,2);
median_mpc_infoshock_sims = median(mpc_br_infoshock,2);
median_mpc2_infoshock_sims = median(mpc_br2_infoshock,2);
median_mpc3_infoshock_sims = median(mpc_br3_infoshock,2);
median_mpc4_infoshock_sims = median(mpc_br4_infoshock,2);

gini_y = gini( ones(length(yi(:,1)),1), yi(:,1));

plfit(yi(:,1))
plfit(wealth_dist_br)
plfit(wealth_dist_re)

%%
periods = 50;
%mpc_nonhtm_infoshock2(1) = 0.04;
%mean_mpc_infoshock_sims(1) = mean_htm_infoshock(1)*1 + (1-mean_htm_infoshock(1))*0.04;

mpc_q5_ma = NaN(periods,1);
mpc_q5_ma(1:2) = mpc_q5_infoshock(1:2);

for i = 3:min(100,periods)
    
    mpc_q5_ma(i) = mean( mpc_q5_infoshock(i-1:i+1));
end

figure
plot([ mean_htm_infoshock(1:periods), mean_mpc_infoshock_sims(1:periods),mpc_q5_ma(1:periods)], 'LineWidth',2.5)
%hold on
%plot( [0.2154*ones(periods,1), 0.1693*ones(periods,1),0.1525*ones(periods,1)], 'k:')
%hold off
xlabel('Periods after shock')
legend('HTM share', 'Mean MPC', 'MPC | top20% assets', 'Location','Northwest')

print -depsc info_shock_bigX.eps


%%


plot([median_mpc_infoshock_sims(2:periods),-median_mpc2_infoshock_sims(2:periods),median_mpc3_infoshock_sims(2:periods),-median_mpc4_infoshock_sims(2:periods)])

figure
plot([wealthsh_top10_infoshock(2:150)])
print -depsc top10_infoshock.eps

figure
plot([wealthsh_top1_infoshock(2:150)])
print -depsc top1_infoshock.eps

figure
plot([wealthsh_bot20_infoshock(2:150)])
print -depsc bot20_infoshock.eps

figure
plot([gini_infoshock(2:150)])
print -depsc gini_infoshock.eps

%% Fiscal stimulus shock

%{
chat_yshock = mean(chat_sims_yshock_br,2);
chat_yshock_big = mean(chat_sims_yshock_big,2);
cstar_yshock = mean(cstar_sims_yshock_re,2);
cstar_yshock_big = mean(cstar_sims_yshock_big,2);

figure
plot( [mean(chat_sims_yshock_br,2)])
%}
